Examples / Quick Start
Try it yourself: All of these examples are using the images provided in the tests directory in the source
Understanding input and output from PyExifTool base methods
Almost all methods in PyExifTool revolve around the usage of two methods from the base exiftool.ExifTool
class.
It is important to understand the ouput from each of these commands, so here’s a quick summary (you can click through to the API to read more details)
Note
Because both methods are inherited by exiftool.ExifToolHelper
and exiftool.ExifToolAlpha
, you can call it from those classes as well.
Input parameters
Both methods take an argument list *args
. Examples:
Note
As a general rule of thumb, if there is an unquoted space on the command line to exiftool, it’s a separate argument to the method in PyExifTool.
If you have a working exiftool command-line but having trouble figuring out how to properly separate the arguments, please refer to the FAQ
Calling directly:
exiftool command-line:
exiftool -XMPToolKit -Subject rose.jpg
PyExifTool:
execute("-XMPToolKit", "-Subject", "rose.jpg")
Using argument unpacking of a list:
exiftool command-line:
exiftool -P -DateTimeOriginal="2021:01:02 03:04:05" -MakerNotes= "spaces in filename.jpg"
PyExifTool:
Note
Parameters which need to be quoted on the command line generally do not need to be quoted in the parameters to PyExifTool. In fact, quoting may have unintended behavior.
In this example, DateTimeOriginal value is not quoted in the parameter to execute().
execute(*["-P", "-DateTimeOriginal=2021:01:02 03:04:05", "-MakerNotes=", "spaces in filename.jpg"])
Getting JSON output using argument unpacking of a list:
exiftool command-line:
exiftool -j -XMP:all -JFIF:JFIFVersion /path/somefile.jpg
PyExifTool:
execute_json(*["-XMP:all", "-JFIF:JFIFVersion", "/path/somefile.jpg"])
Output values
exiftool.ExifTool.execute_json()
Returns a
list
ofdict
Each
dict
is a result from a fileEach
dict
contains a key “SourceFile” which points to the relative or absolute file path of fileAll other keys/value pairs are requested metadata
-
Returns a
str
Typically used for setting tags as no values are returned in that case.
ExifToolHelper
Using methods provided by exiftool.ExifToolHelper
:
ExifToolHelper provides some of the most commonly used operations most people use exiftool for
Exceptions
By default, ExifToolHelper has some built-in error checking, making the methods safer to use than calling the base methods directly.
Warning
While “safer”, the error checking isn’t fool-proof. There are a lot of cases where exiftool just silently ignores bad input and doesn’t indicate an error.
Example using get_tags() on a list which includes a non-existent file
ExifToolHelper with error-checking, using
exiftool.ExifToolHelper.get_tags()
from exiftool import ExifToolHelper with ExifToolHelper() as et: print(et.get_tags( ["rose.jpg", "skyblue.png", "non-existent file.tif"], tags=["FileSize"] ))
Output:
Traceback (most recent call last): File "T:\example.py", line 7, in <module> et.get_tags(["rose.jpg", "skyblue.png", "non-existent file.tif"], tags=["FileSize"]) File "T:\pyexiftool\exiftool\helper.py", line 353, in get_tags ret = self.execute_json(*exec_params) File "T:\pyexiftool\exiftool\exiftool.py", line 1030, in execute_json result = self.execute("-j", *params) # stdout File "T:\pyexiftool\exiftool\helper.py", line 119, in execute raise ExifToolExecuteError(self._last_status, self._last_stdout, self._last_stderr, params) exiftool.exceptions.ExifToolExecuteError: execute returned a non-zero exit status: 1
ExifTool only, without error checking, using
exiftool.ExifTool.execute_json()
(Note how the missing file is silently ignored and doesn’t show up in returned list.)from exiftool import ExifToolHelper with ExifToolHelper() as et: print(et.get_tags( ["rose.jpg", "skyblue.png", "non-existent file.tif"], tags=["FileSize"] ))
Output:
[{'SourceFile': 'rose.jpg', 'File:FileSize': 4949}, {'SourceFile': 'skyblue.png', 'File:FileSize': 206}]
Example using
exiftool.ExifToolHelper.get_tags()
with a typo. Let’s say you wanted toget_tags()
, but accidentally copy/pasted something and left a=
character behind (deletes tag rather than getting!)…Using
exiftool.ExifToolHelper.get_tags()
from exiftool import ExifToolHelper with ExifToolHelper() as et: print(et.get_tags(["skyblue.png"], tags=["XMP:Subject=hi"]))
Output:
Traceback (most recent call last): File "T:\example.py", line 7, in <module> print(et.get_tags(["skyblue.png"], tags=["XMP:Subject=hi"])) File "T:\pyexiftool\exiftool\helper.py", line 341, in get_tags self.__class__._check_tag_list(final_tags) File "T:\pyexiftool\exiftool\helper.py", line 574, in _check_tag_list raise ExifToolTagNameError(t) exiftool.exceptions.ExifToolTagNameError: Invalid Tag Name found: "XMP:Subject=hi"
Using
exiftool.ExifTool.execute_json()
. It still raises an exception, but more cryptic and difficult to debugfrom exiftool import ExifTool with ExifTool() as et: print(et.execute_json(*["-XMP:Subject=hi"] + ["skyblue.png"]))
Output:
Traceback (most recent call last): File "T:\example.py", line 7, in <module> print(et.execute_json(*["-XMP:Subject=hi"] + ["skyblue.png"])) File "T:\pyexiftool\exiftool\exiftool.py", line 1052, in execute_json raise ExifToolOutputEmptyError(self._last_status, self._last_stdout, self._last_stderr, params) exiftool.exceptions.ExifToolOutputEmptyError: execute_json expected output on stdout but got none
Using
exiftool.ExifTool.execute()
. No errors, but you have now written to the file instead of reading from it!from exiftool import ExifTool with ExifTool() as et: print(et.execute(*["-XMP:Subject=hi"] + ["skyblue.png"]))
Output:
1 image files updated
ExifTool
Using methods provided by exiftool.ExifTool
Calling execute() or execute_json() provides raw functionality for advanced use cases. Use with care!