matplotlib可以向保存的图形添加元数据吗? [英] Can matplotlib add metadata to saved figures?
问题描述
我希望能够确定使用matplotlib创建的图形的来源,即知道我的代码和数据的哪个版本创建了这些图形. (有关出处的更多信息,请参见本文).
I want to be able to ascertain the provenance of the figures I create using matplotlib, i.e. to know which version of my code and data created these figures. (See this essay for more on provenance.)
我想,最直接的方法是将代码和数据的修订号添加到保存的图形的元数据中,或者作为后记文件中的注释.
I imagine the most straightforward approach would be to add the revision numbers of the code and data to the metadata of the saved figures, or as comments in a postscript file for example.
在Matplotlib中,有什么简单的方法可以做到这一点吗? savefig
函数似乎不具备此功能,但是有人提出了可行的解决方案吗?
Is there any easy way to do this in Matplotlib? The savefig
function doesn't seem to be capable of this but has someone come up with a workable solution?
推荐答案
I don't know of a way using matplotlib
, but you can add metadata to png's with PIL
:
f = "test.png"
METADATA = {"version":"1.0", "OP":"ihuston"}
# Create a sample image
import pylab as plt
import numpy as np
X = np.random.random((50,50))
plt.imshow(X)
plt.savefig(f)
# Use PIL to save some image metadata
from PIL import Image
from PIL import PngImagePlugin
im = Image.open(f)
meta = PngImagePlugin.PngInfo()
for x in METADATA:
meta.add_text(x, METADATA[x])
im.save(f, "png", pnginfo=meta)
im2 = Image.open(f)
print im2.info
这给出了:
{'version': '1.0', 'OP': 'ihuston'}
这篇关于matplotlib可以向保存的图形添加元数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!