Skip to content Skip to sidebar Skip to footer

Convert .h5 File To .jpg With Python

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

Solution 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
fromPILimportImage

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()

Post a Comment for "Convert .h5 File To .jpg With Python"