python - 如何将 matplotlib 图保存到 hdf5 文件?
问题描述
我正在处理测量数据,并在接下来的过程中创建它们的图。(更具体地说,它即将显示旋转轴的轨道。)因此,我将创建许多图,并且我想将它们安全地保存到 hdf5 文件中。我知道的“解决方法”是:
- 将文件保存到 .jgp
- 用opencv阅读
- 将该数组写入 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 的解决方案。
此致
马吕斯
解决方案
推荐阅读
- python - 如何将这 2 个日期/时间列转换为 1?
- go - 从 golang 包中导出接口而不是结构
- python - SQLAlchemy 自定义关键字参数因 TypeError 失败:'argument' is is an invalid keyword argument for this function
- javascript - 为什么必须 json.parse(buffer.concat(chunks)) 两次?
- php - 处理多个表的sql选择查询
- python - 散景悬停阿尔法和颜色
- python - 让 Python RegEx 更简洁
- python - 检查二维数组是否包含 Numpy 中的特定一维数组
- asp.net-core - 如何解决 aspnet core 2.1 中的版本冲突?(2.1.1 >= 2.1.0-rc1-final)
- powershell - 导入模块速度差异