问题
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.
回答1:
Key ingredients:
h5py to read the h5 file. Determine the format of your image and use PIL.
Let us suppose it's RGB format (https://support.hdfgroup.org/products/java/hdfview/UsersGuide/ug06imageview.html)
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()
来源:https://stackoverflow.com/questions/52394252/convert-h5-file-to-jpg-with-python