Python:Images: Difference between revisions
Jump to navigation
Jump to search
Created page with "Category:Python Images can have meta-data embedded. Most used is Exif[https://nl.wikipedia.org/wiki/Exchangeable_image_file_format], other formats are IPT[https://nl.wikipedia.org/wiki/IPTC] and XMP[https://nl.wikipedia.org/wiki/Extensible_Metadata_Platform]. ;import pyexiv2 :Module that handles all types of metadata <syntaxhighlight lang=python> import pyexiv2 metadata = pyexiv2.ImageMetadata(imagefile) metadata.read() print(f"Metadata for {imagefile}") for key,..." |
No edit summary |
||
| Line 3: | Line 3: | ||
Images can have meta-data embedded. Most used is Exif[https://nl.wikipedia.org/wiki/Exchangeable_image_file_format], other formats are IPT[https://nl.wikipedia.org/wiki/IPTC] and XMP[https://nl.wikipedia.org/wiki/Extensible_Metadata_Platform]. | Images can have meta-data embedded. Most used is Exif[https://nl.wikipedia.org/wiki/Exchangeable_image_file_format], other formats are IPT[https://nl.wikipedia.org/wiki/IPTC] and XMP[https://nl.wikipedia.org/wiki/Extensible_Metadata_Platform]. | ||
;import | It appears that [https://exiftool.org/ exiftool] is the way to go. [https://pypi.org/project/PyExifTool/ PyExifTool] is a python wrapper for it. | ||
: | |||
In Debian exiftool is provided by libimage-exiftool-perl. That is automatically selected when you <code>apt install exiftool</code>. | |||
PyExifTool is not available as a Debian package. Install it in a [[Python#Virtual_environment]] | |||
;from exiftool import ExfiToolHelper as et | |||
:ExifToolHelper is a wrapper for ExifTool with some helper functionality. | |||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
import | from exiftool import ExfiToolHelper | ||
import json | |||
with ExifToolHelper() as et: | |||
metadata = et.get_metadata(filename) | |||
for ifd in metadata: | |||
if isinstance(ifd, dict): | |||
for tag, value in ifd.items(): | |||
print(f"{tag}: {value}") | |||
else: | |||
print(f"ifd {ifd}") | |||
# Set IPTC:Keywords | |||
with ExifToolHelper() as et: | |||
et.set_tags([filename], | |||
tags={"Keywords": ["sunny", "nice day", "cool", "awesome"]} | |||
) | |||
# Set XMP:Subject | |||
with ExifToolHelper() as et: | |||
et.set_tags([filename], | |||
tags={"Subject": ["Holiday", "Spain"]} | |||
) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 20:06, 14 July 2025
Images can have meta-data embedded. Most used is Exif[1], other formats are IPT[2] and XMP[3].
It appears that exiftool is the way to go. PyExifTool is a python wrapper for it.
In Debian exiftool is provided by libimage-exiftool-perl. That is automatically selected when you apt install exiftool.
PyExifTool is not available as a Debian package. Install it in a Python#Virtual_environment
- from exiftool import ExfiToolHelper as et
- ExifToolHelper is a wrapper for ExifTool with some helper functionality.
from exiftool import ExfiToolHelper
import json
with ExifToolHelper() as et:
metadata = et.get_metadata(filename)
for ifd in metadata:
if isinstance(ifd, dict):
for tag, value in ifd.items():
print(f"{tag}: {value}")
else:
print(f"ifd {ifd}")
# Set IPTC:Keywords
with ExifToolHelper() as et:
et.set_tags([filename],
tags={"Keywords": ["sunny", "nice day", "cool", "awesome"]}
)
# Set XMP:Subject
with ExifToolHelper() as et:
et.set_tags([filename],
tags={"Subject": ["Holiday", "Spain"]}
)