【问题标题】:How can I visualise an image in h5 format data?如何以 h5 格式数据可视化图像?
【发布时间】:2017-05-28 09:14:18
【问题描述】:
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">

【问题讨论】:

    标签: python image image-processing hdf5 h5py


    【解决方案1】:
    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)
    

    【讨论】:

      【解决方案2】:

      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) 提供类似的图像保存功能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-23
        • 2018-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-25
        • 1970-01-01
        相关资源
        最近更新 更多