简体   繁体   English

如何使用 matplotlib 向易于出现在 Windows 10 中的图像添加元数据

[英]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:我正在尝试使用以下方法将元数据添加到从 matplotlib 中的图形保存的图像中:

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.但是,在 Windows 10 中检查图像的属性时,这些信息都没有出现。我知道我在 plt.savefig() 中指定的元数据存在(我用其他软件检查过),但我希望能够有一个元数据只需将光标放在图像上即可在 Windows 10 中显示。

If possible, try manually adding camera data to a file using the windows 10 interface.如果可能,请尝试使用 Windows 10 界面手动将相机数据添加到文件中。 Use a tiff file to do this (You'd be surprised how similar the metadata works between tiff and png these days).使用 tiff 文件来执行此操作(如今,您会惊讶于 tiff 和 png 之间元数据的工作方式如此相似)。 Once you do this, use whatever tool you have to read the camera maker data from that tiff file.完成此操作后,请使用您必须使用的任何工具从该 tiff 文件中读取相机制造商数据。 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.我只在 Windows 7 中使用 py3exiv2 完成了此操作,但是如果您遇到困难,此策略应该会让您有所行动。

The savefig documentation states: savefig文档指出:

metadata : dict, optional metadata :字典,可选

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 . 'png' with Agg backend:见print_png的参数元数据。
  • 'pdf' with pdf backend: See the parameter metadata of PdfPages . 'pdf' with pdf backend:参见PdfPages的参数元数据。
  • 'eps' and 'ps' with PS backend: Only 'Creator' is supported.带有 PS 后端的“eps”和“ps”:仅支持“Creator”。

This means the metadata argument is ignored for jpg images .这意味着jpg 图像的metadata参数将被忽略

In case of jpg images you would need to use the pil_kwargs argument instead.如果是 jpg 图像,则需要改用pil_kwargs参数。 Valid PIL/pillow arguments are in the Pillow documentation and one of them is "exif" .有效的 PIL/枕头参数在Pillow 文档中,其中之一是"exif" It would expect the exif as raw bytes.它希望 exif 作为原始字节。
Therefore one can use a package like piexif to povide the data.因此可以使用像piexif这样的包来提供数据。

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.这会将 exif 信息保存到文件中。 Windows may or may not recognize it; Windows 可能会也可能不会识别它; so that is a totally different topic.所以这是一个完全不同的话题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM