PyExifTool – A Python wrapper for Phil Harvey’s ExifTool

PyExifTool is a Python library to communicate with an instance of Phil Harvey’s excellent ExifTool command-line application. The library provides the class ExifTool that runs the command-line tool in batch mode and features methods to send commands to that program, including methods to extract meta-information from one or more image files. 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.

The source code can be checked out from the github repository with

git clone git://github.com/smarnach/pyexiftool.git

Alternatively, you can download a tarball. There haven’t been any releases yet.

PyExifTool is licenced under GNU GPL version 3 or later.

Example usage:

import exiftool

files = ["a.jpg", "b.png", "c.tif"]
with exiftool.ExifTool() as et:
    metadata = et.get_metadata_batch(files)
for d in metadata:
    print("{:20.20} {:20.20}".format(d["SourceFile"],
                                     d["EXIF:DateTimeOriginal"]))
class exiftool.ExifTool(executable_=None)

Run the exiftool command-line tool and communicate to it.

You can pass the file name of the exiftool executable as an argument to the constructor. The default value exiftool will only work if the executable is in your PATH.

Most methods of this class are only available after calling start(), which will actually launch the subprocess. To avoid leaving the subprocess running, make sure to call terminate() method when finished using the instance. This method will also be implicitly called when the instance is garbage collected, but there are circumstance when this won’t ever happen, so you should not rely on the implicit process termination. Subprocesses won’t be automatically terminated if the parent process exits, so a leaked subprocess will stay around until manually killed.

A convenient way to make sure that the subprocess is terminated is to use the ExifTool instance as a context manager:

with ExifTool() as et:
    ...

Warning

Note that there is no error handling. Nonsensical options will be silently ignored by exiftool, so there’s not much that can be done in that regard. You should avoid passing non-existent files to any of the methods, since this will lead to undefied behaviour.

running

A Boolean value indicating whether this instance is currently associated with a running subprocess.

execute(*params)

Execute the given batch of parameters with exiftool.

This method accepts any number of parameters and sends them to the attached exiftool process. The process must be running, otherwise ValueError is raised. The final -execute necessary to actually run the batch is appended automatically; see the documentation of start() for the common options. The exiftool output is read up to the end-of-output sentinel and returned as a raw bytes object, excluding the sentinel.

The parameters must also be raw bytes, in whatever encoding exiftool accepts. For filenames, this should be the system’s filesystem encoding.

Note

This is considered a low-level method, and should rarely be needed by application developers.

execute_json(*params)

Execute the given batch of parameters and parse the JSON output.

This method is similar to execute(). It automatically adds the parameter -j to request JSON output from exiftool and parses the output. The return value is a list of dictionaries, mapping tag names to the corresponding values. All keys are Unicode strings with the tag names, including the ExifTool group name in the format <group>:<tag>. The values can have multiple types. All strings occurring as values will be Unicode strings.

The parameters to this function must be either raw strings (type str in Python 2.x, type bytes in Python 3.x) or Unicode strings (type unicode in Python 2.x, type str in Python 3.x). Unicode strings will be encoded using system’s filesystem encoding. This behaviour means you can pass in filenames according to the convention of the respective Python version – as raw strings in Python 2.x and as Unicode strings in Python 3.x.

get_metadata(filename)

Return meta-data for a single file.

The returned dictionary has the format described in the documentation of execute_json().

get_metadata_batch(filenames)

Return all meta-data for the given files.

The return value will have the format described in the documentation of execute_json().

get_tag(tag, filename)

Extract a single tag from a single file.

The return value is the value of the specified tag, or None if this tag was not found in the file.

get_tag_batch(tag, filenames)

Extract a single tag from the given files.

The first argument is a single tag name, as usual in the format <group>:<tag>.

The second argument is an iterable of file names.

The return value is a list of tag values or None for non-existent tags, in the same order as filenames.

get_tags(tags, filename)

Return only specified tags for a single file.

The returned dictionary has the format described in the documentation of execute_json().

get_tags_batch(tags, filenames)

Return only specified tags for the given files.

The first argument is an iterable of tags. The tag names may include group names, as usual in the format <group>:<tag>.

The second argument is an iterable of file names.

The format of the return value is the same as for execute_json().

start()

Start an exiftool process in batch mode for this instance.

This method will issue a UserWarning if the subprocess is already running. The process is started with the -G and -n as common arguments, which are automatically included in every command you run with execute().

terminate()

Terminate the exiftool process of this instance.

If the subprocess isn’t running, this method will do nothing.

exiftool.executable = 'exiftool'

The name of the executable to run.

If the executable is not located in one of the paths listed in the PATH environment variable, the full path should be given here.

exiftool.fsencode(filename)

Encode filename to the filesystem encoding with ‘surrogateescape’ error handler, return bytes unchanged. On Windows, use ‘strict’ error handler if the file system encoding is ‘mbcs’ (which is the default encoding).

This Page