Python:Images: Difference between revisions

From wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 10: Line 10:


;from exiftool import ExfiToolHelper
;from exiftool import ExfiToolHelper
:ExifToolHelper is a wrapper for ExifTool with some helper functionality.
:ExifToolHelper is a wrapper for ExifTool with some helpful functionality.
;et = ExifToolHelper()
;et = ExifToolHelper()
:Instantiate an ExifToolHelper object. It opens an instance of exiftool in batch mode. Exiftool in closed when the object is terminated
:Instantiate an ExifToolHelper object. It opens an instance of exiftool in batch mode. Exiftool in closed when the object is terminated
Line 16: Line 16:
:Destroying the ExifToolHelper object terminates the exiftool instance.
:Destroying the ExifToolHelper object terminates the exiftool instance.
;<nowiki>with ExifToolHelper() as et:</nowiki>
;<nowiki>with ExifToolHelper() as et:</nowiki>
:Auto terminate the ExifToolHelper object at the end of the block
: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>
from exiftool import ExfiToolHelper
from exiftool import ExfiToolHelper

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"]})