Convert .h5 file to .jpg with Python

吃可爱长大的小学妹 提交于 2020-01-04 01:59:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!