Python:Images: Difference between revisions

From wiki
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
 
(3 intermediate revisions by the same user not shown)
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 pyexiv2
It appears that [https://exiftool.org/ exiftool] is the way to go. [https://pypi.org/project/PyExifTool/ PyExifTool] is a python wrapper for it.
:Module that handles all types of metadata


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
:ExifToolHelper is a wrapper for ExifTool with some helpful functionality.
;et = ExifToolHelper()
:Instantiate an ExifToolHelper object. It opens an instance of exiftool in batch mode. Exiftool in closed when the object is terminated
;et.terminate()
:Destroying the ExifToolHelper object terminates the exiftool instance.
;<nowiki>with ExifToolHelper() as et:</nowiki>
:Automatically terminate the ExifToolHelper object at the end of the block
;et.get_metadata(<filenames>)
:Get all metadata from <filenames>, <filenames> can be a single path or a list of paths
:Returns a list of dictionaries {<tagname>:<tagvalue>,..}. <tagvalue> can be any type.
;<nowiki>et.set_tags(<filenames>, tags={"Subject": ["Holiday", "Spain", "Trees"]})</nowiki>
:Set the XMP:Subject tag in <filenames>.
Example code:
<syntaxhighlight lang=python>
<syntaxhighlight lang=python>
import pyexiv2
from exiftool import ExfiToolHelper
import json
 
with ExifToolHelper() as et:
    metadatalist = et.get_metadata(filename)
    for metadata in metadatalist:
        for tag, value in metadata.items():
            print(f"{tag}: {value}")
 
# Set IPTC:Keywords (not shown by Debian eog (Afbeeldingsweergave in Dutch))
with ExifToolHelper() as et:
    et.set_tags([filename], tags={"Keywords": ["sunny", "nice day", "cool", "awesome"]})


metadata = pyexiv2.ImageMetadata(imagefile)
# Set XMP:Subject  (XMP is newer than IPTC, I prefer to use this)
metadata.read()
with ExifToolHelper() as et:
print(f"Metadata for {imagefile}")
    et.set_tags([filename], tags={"Subject": ["Holiday", "Spain"]})
for key, value in metadata.items():
    print(f"{key}, {value}")
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 13:32, 20 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
ExifToolHelper is a wrapper for ExifTool with some helpful functionality.
et = ExifToolHelper()
Instantiate an ExifToolHelper object. It opens an instance of exiftool in batch mode. Exiftool in closed when the object is terminated
et.terminate()
Destroying the ExifToolHelper object terminates the exiftool instance.
with ExifToolHelper() as et:
Automatically terminate the ExifToolHelper object at the end of the block
et.get_metadata(<filenames>)
Get all metadata from <filenames>, <filenames> can be a single path or a list of paths
Returns a list of dictionaries {<tagname>:<tagvalue>,..}. <tagvalue> can be any type.
et.set_tags(<filenames>, tags={"Subject": ["Holiday", "Spain", "Trees"]})
Set the XMP:Subject tag in <filenames>.

Example code:

from exiftool import ExfiToolHelper
import json

with ExifToolHelper() as et:
    metadatalist = et.get_metadata(filename)
    for metadata in metadatalist:
        for tag, value in metadata.items():
            print(f"{tag}: {value}")

# Set IPTC:Keywords (not shown by Debian eog (Afbeeldingsweergave in Dutch))
with ExifToolHelper() as et:
    et.set_tags([filename], tags={"Keywords": ["sunny", "nice day", "cool", "awesome"]})

# Set XMP:Subject  (XMP is newer than IPTC, I prefer to use this)
with ExifToolHelper() as et:
    et.set_tags([filename], tags={"Subject": ["Holiday", "Spain"]})