Can matplotlib add metadata to saved figures?
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 imageimport pylab as pltimport numpy as npX = np.random.random((50,50))plt.imshow(X)plt.savefig(f)# Use PIL to save some image metadatafrom PIL import Imagefrom PIL import PngImagePluginim = 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
This gives:
{'version': '1.0', 'OP': 'ihuston'}
If you are interested in PDF files, then you can have a look at the matplotlib module matplotlib.backends.backend_pdf
. At this link there is a nice example of its usage, which could be "condensed" into the following:
import pylab as plimport numpy as npfrom matplotlib.backends.backend_pdf import PdfPagespdffig = PdfPages('figure.pdf')x=np.arange(10)pl.plot(x)pl.savefig(pdffig, format="pdf")metadata = pdffig.infodict()metadata['Title'] = 'Example'metadata['Author'] = 'Pluto'metadata['Subject'] = 'How to add metadata to a PDF file within matplotlib'metadata['Keywords'] = 'PdfPages example'pdffig.close()
As of matplotlib version 2.1.0, the savefig command accepts the keyword argument metadata
. You pass in a dictionary with string key/value pairs to be saved.
This only fully works with the 'agg'
backend for PNG files.
For PDF and PS files you can use a pre-defined list of tags.