Reading and writing IPTC metadata in Python using pyexiftool

The tool that we recommend the most for command-line use is Phil Harvey’s exiftool (exiftool.org).

Pyexiftool is a simple wrapper around the tool.

Note that pyexiftool requires the Perl exiftool to be installed on the host machine and simply uses Python exec() to run the command-line exiftool and process the results.
This may cause problems if you don’t have enough control over your environment to install exiftool.

Installing pyexiftool for Python is simple if your system already has exiftool installed:

python -m pip install -U pyexiftool

As the docs say, “Since exiftool is run in batch mode, only a single instance needs to be launched and can be reused for many queries. This is much more efficient than launching a separate process for every single query.”

On this page:

Examples of reading IPTC metadata in Python using pyexiftool

To get you started quickly, here are some examples of things that can be done with it regarding IPTC metadata (we cover writing these fields below)

View all embedded metadata for an image file

Simply run exiftool with the file name / path to the image file that you wish to investigate:

% python >>> import exiftool ... with exiftool.ExifTool() as et: ... print(et.execute("-XMP:all", "test-image.jpg")) ...

which will extract all XMP-encoded metadata including IPTC.

Another approach is to use the ExiftoolHelper feature of pyexiftool, which improves error handling:

% python >>> from exiftool import ExifToolHelper ... with ExifToolHelper() as et: ... for d in et.get_metadata("test-image.jpg"): ... for k, v in d.items(): ... print(f"{k} = {v}") ...

This will simply display all metadata (IPTC, Exif and other formats) that can be extracted from the file.

View a single-valued metadata property in an image file

Exiftool can extract any single property from a media file. To find out which tag to use, first consult the IPTC Photo Metadata Standard Specification to get the XMP tag used, then consult the exiftool XMP tags page to determine how to specify that tag for exiftool.

Show only the Digital Source Type value

Show only the Credit Line value

View multi-valued properties in image files

Here’s an example of viewing a non-structured but multi-valued property:

Show the creator value(s)

By default, exiftool shows multi-valued properties as comma-separated values on the same line:

To make it clear that this is in fact a repeatable value, you can ask for JSON-encoded output using execute_json():

View a structured property for an image file

Some properties (such as Licensor which is used for Google’s Licensable feature) are structured, which means they contain sub-properties. In this case, running the simple et.execute_json("-XMP-plus:Licensor", "test-image.jpg")) returns no results.

The secret is to pass the -struct option to the exiftool constructor:

View all properties read by Google Image Search for the Licensable badge feature

To read more than one property from a file, simply specify multiple Exiftool tags, each prefixed with a dash/hyphen character.

View all properties read by Google Image Search for Licensable badge and Synthetic Media features

Examples of writing IPTC metadata in Python using pyexiftool

The same exiftool XMP tags can be used to write metadata to image files on the command line.

Writing Digital Source Type for synthetic media

Note that the value can be any text string, but we recommend using a full URI from the IPTC Digital Source Type vocabulary:

(Note that Google’s documentation on Digital Source Type for synthetic media specifies using only the ID, not the full URI. So if you are writing software to read these values, we recommend that you support both the fully-expanded URI and the short ID.)

Writing Creator and Credit Line metadata

The contents of these fields will be displayed in the Google Images search results details panel.

Writing Licensor URL metadata

This triggers the “Get this image on…” text in Google Images search results.

Writing Web Statement of Rights metadata

This triggers the “Licensable” badge in Google image search results.

Appending to an existing set of repeatable tags

exiftool allows appending to a repeatable list of tags, for example Image Creator (dc:creator). This functionality also works through pyexiftool:

Would you like more examples? Just ask!

If you would like to see other exiftool examples, or if you have suggestions of exiftool incantations that you find useful and you would like to share them with others, please let us know!