首页 > 解决方案 > 如何将 matplotlib 图保存到 hdf5 文件?

问题描述

我正在处理测量数据,并在接下来的过程中创建它们的图。(更具体地说,它即将显示旋转轴的轨道。)因此,我将创建许多图,并且我想将它们安全地保存到 hdf5 文件中。我知道的“解决方法”是:

  1. 将文件保存到 .jgp
  2. 用opencv阅读
  3. 将该数组写入 hdf5

这很好用,但会在我的工作字典中造成混乱。

如果有人只想保护一个或两个地块,这是一个代码示例:

    fig.savefig(some_plot.jpg)  # saves the plot to working discretionary as 'some_plot.jpg'
    image = cv2.imread(some_plot.jpg)  # reads the created .jpg as array 
    g = f.create_dataset(some_path, data=image)  # creates dataset in the hdf5 and safes the 
    image array to it
    # add !!important!! image attributes , i got these from another forum but if you convert a 
    # image in hdf5 and read the attributes you can create them by yourself
    # futher information is in the hdf5 documentation (hdfgroup.org --> HDF5 Image and Palette 
    Specification)
    g.attrs.create('CLASS', 'IMAGE', dtype='S6')                                        
    g.attrs.create('IMAGE_MINMAXRANGE', [0, 255], dtype=np.uint8)
    g.attrs.create('IMAGE_SUBCLASS', 'IMAGE_TRUECOLOR', dtype='S16')
    g.attrs.create('IMAGE_VERSION', '1.2', dtype='S4')
    g.attrs.create('INTERLACE_MODE', 'INTERLACE_PIXEL', dtype='S16')

所以现在我的问题是这段代码为每个情节创建了一个 .jpg 图像,这会造成混乱。有没有一种方法可以将图片转换为 numpy 数组而无需将其保存为图像。(我可以在将图片保存到 hdf5 后删除它,但我想避免这种情况。) matplotlib.pyplot 有一个内置的 imread() 函数,我将在未来尝试安全使用 opencv。我希望有人知道将绘图图像直接保存到 hdf5 的解决方案。

此致

马吕斯

标签: pythonimagematplotlibhdf5

解决方案


推荐阅读