【问题标题】:Convert .h5 file to .jpg with Python使用 Python 将 .h5 文件转换为 .jpg
【发布时间】:2019-02-22 22:38:13
【问题描述】:

我目前有一个包含灰度图像的 .h5 文件。我需要将其转换为 .jpg。

有人有这方面的经验吗?

注意:我可以将 h5 文件转换为 numpy 数组,然后使用 pypng 之类的外部库将其转换为 png。但我想知道是否有更有效的方法来转换为图像,最好是.jpg。

【问题讨论】:

    标签: python jpeg hdf5 image-conversion


    【解决方案1】:

    主要成分:

    h5py 读取 h5 文件。 确定图像的格式并使用 PIL。

    假设它是 RGB 格式 (https://support.hdfgroup.org/products/java/hdfview/UsersGuide/ug06imageview.html)

    假设你的图片位于 Photos/Image 1 那么你可以这样做。

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

    【讨论】:

    • array = hdf["Photos/Image 1"][:]h5py 将数据集加载为 numpy 数组。不需要array(list(...))) 跳舞。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    相关资源
    最近更新 更多