I currently have a .h5 file containing grayscale imagery. I need to convert it to a .jpg.
Does anybody have any experience with this?
Note: I could possible convert the h5 file to a numpy array and then use an external library like pypng to convert that to a png. But I am wondering if there is a more efficient way to convert to an image, and preferrably a .jpg.
答案 0 :(得分:3)
关键成分:
h5py读取h5文件。 确定图像格式并使用PIL。
让我们假设它是RGB格式(https://support.hdfgroup.org/products/java/hdfview/UsersGuide/ug06imageview.html)
假设您的图片位于“照片/图片1”,就可以了。
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()