使用Python将.h5文件转换为.jpg [英] Convert .h5 file to .jpg with Python
本文介绍了使用Python将.h5文件转换为.jpg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前有一个.h5文件,其中包含灰度图像.我需要将其转换为.jpg.
I currently have a .h5 file containing grayscale imagery. I need to convert it to a .jpg.
有人对此有任何经验吗?
Does anybody have any experience with this?
注意:我可以将h5文件转换为numpy数组,然后使用pypng之类的外部库将其转换为png.但我想知道是否有更有效的方法来转换为图像,最好是转换为.jpg.
推荐答案
关键要素:
h5py读取h5文件. 确定图像的格式并使用PIL.
h5py to read the h5 file. Determine the format of your image and use PIL.
让我们假设它是RGB格式( https://support. hdfgroup.org/products/java/hdfview/UsersGuide/ug06imageview.html )
Let us suppose it's RGB format (https://support.hdfgroup.org/products/java/hdfview/UsersGuide/ug06imageview.html)
假设您的图片位于照片/图片1",就可以了.
Suppose your image is located at Photos/Image 1 then you can do.
import h5py
import numpy as np
from PIL import Image
hdf = h5py.File("Sample.h5",'r')
array = hdf["Photos/Image 1"][:]
img = Image.fromarray(array.astype('uint8'), 'RGB')
img.save("yourimage.thumbnail", "JPEG")
img.show()
这篇关于使用Python将.h5文件转换为.jpg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文