且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Python将.h5文件转换为.jpg

更新时间:2023-01-18 14:21:30

关键要素:

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