[英]How can I "visualize" 3D image stored (h5py) .h5 format in python?
[英]How can I visualise an image in h5 format data?
import h5py
f = h5py.File('the_file.h5', 'r')
one_data = f['key']
print(one_data.shape)
print(one_data.dtype)
print(one_data)
我使用上面的代码打印信息。 打印结果为:
(320, 320, 3)
uint8
<HDF5 dataset "1458552843.750": shape (320, 320, 3), type "|u1">
import cv2
import numpy as np
import h5py
f = h5py.File('the_file.h5', 'r')
dset = f['key']
data = np.array(dset[:,:,:])
file = 'test.jpg'
cv2.imwrite(file, data)
jet提供的解决方案工作正常,但缺点是需要包含OpenCV(cv2)。 如果您没有将OpenCV用于其他任何事情,安装/包含它仅仅是为了保存文件有点过分。 或者,您可以使用imageio.imwrite
( doc ),例如:
import imageio
import numpy as np
import h5py
f = h5py.File('the_file.h5', 'r')
dset = f['key']
data = np.array(dset[:,:,:])
file = 'test.png' # or .jpg
imageio.imwrite(file, data)
安装imageio就像pip install imageio
一样简单。
此外, matplotlib.image.imsave
( doc )提供了类似的图像保存功能。
它可以更简单:
pip install Pillow h5py
然后
import h5py
from PIL import Image
f = h5py.File('the_file.h5', 'r')
dset = f['key'][:]
img = Image.fromarray(dset.astype("uint8"), "RGB")
img.save("test.png")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.