How to add metadata to images that easily appear in Windows 10 with matplotlib

I am trying to add metadata to images saved from graphs in matplotlib using:

plt.savefig("image1.jpeg", metadata = {"Camera maker": "XYZ"}

However, none of that info appears while inspecting the image's properties in Windows 10. I know the metadata I specified in the plt.savefig() exists (I checked it with additional software), but I want to be able to have a metadata that displays in Windows 10 just by letting the cursor on the image.

Upvotes: 1

Views: 1180

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339120

The savefig documentation states:

metadata : dict, optional

Key/value pairs to store in the image metadata. The supported keys and defaults depend on the image format and backend:

  • 'png' with Agg backend: See the parameter metadata of print_png.
  • 'pdf' with pdf backend: See the parameter metadata of PdfPages.
  • 'eps' and 'ps' with PS backend: Only 'Creator' is supported.

This means the metadata argument is ignored for jpg images.

In case of jpg images you would need to use the pil_kwargs argument instead. Valid PIL/pillow arguments are in the Pillow documentation and one of them is "exif". It would expect the exif as raw bytes.
Therefore one can use a package like piexif to povide the data.

It could look like this:

import piexif
import matplotlib.pyplot as plt

fig, ax = plt.subplots()


exif_dict = {"0th" : {piexif.ImageIFD.Make: u"Canon",}}
exif_byte = piexif.dump(exif_dict)

plt.savefig("image1.jpg", pil_kwargs = {"exif" : exif_byte} )

plt.show()

This will save the exif information into the file. Windows may or may not recognize it; so that is a totally different topic.

Upvotes: 1

wimworks
wimworks

Reputation: 323

If possible, try manually adding camera data to a file using the windows 10 interface. Use a tiff file to do this (You'd be surprised how similar the metadata works between tiff and png these days). Once you do this, use whatever tool you have to read the camera maker data from that tiff file. That will let you know what keys windows actually saves that information to. Then just use that same behavior in your code, and it should work.

I've only done this with py3exiv2 in windows 7, but this strategy should get you moving if you're stuck.

Upvotes: 1

Related Questions