PyExifTool

0.5.6 · active · verified Fri Apr 17

PyExifTool provides a convenient Python interface to Phil Harvey's ExifTool, a powerful command-line application for reading, writing, and editing metadata in a wide variety of file formats. The current version is 0.5.6, and it maintains an active release cadence with new features and improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate ExifTool using a context manager and read metadata from an image file. It includes a fallback to create a dummy file for immediate execution, but real EXIF data requires a valid image. It also highlights the requirement for the `exiftool` command-line utility.

import os
from pyexiftool import ExifTool

# IMPORTANT: Ensure the 'exiftool' command-line utility is installed
# and accessible on your system PATH.
# For Debian/Ubuntu: sudo apt-get install libimage-exiftool-perl
# For macOS: brew install exiftool
# More info: https://exiftool.org/ 

# Create a dummy image file for demonstration if it doesn't exist
image_file = "example.jpg"
if not os.path.exists(image_file):
    with open(image_file, "w") as f:
        f.write("Simulated JPEG content\n")
    print(f"Created dummy file: {image_file}. Replace with a real image for meaningful EXIF data.")

try:
    # Use ExifTool as a context manager to ensure proper process handling
    with ExifTool() as et:
        # Read all metadata from the image file
        metadata = et.get_metadata(image_file)
        print(f"\nMetadata for {image_file}:")
        for tag, value in metadata.items():
            print(f"  {tag}: {value}")

        # Example of writing metadata (uncomment and use a real image if needed)
        # For writing, ensure the image_file is a real image and you have write permissions.
        # new_tags = {"Exif.Image.Artist": "Python Bot", "Exif.Image.Copyright": "2023 Example Corp"}
        # et.set_tags(new_tags, [image_file])
        # print(f"\nUpdated metadata for {image_file}")
        # updated_metadata = et.get_metadata(image_file)
        # print("Updated Artist:", updated_metadata.get("Exif.Image.Artist"))

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure 'exiftool' is installed and your image file path is correct.")
finally:
    # Clean up the dummy file if it was created
    if os.path.exists(image_file) and os.path.getsize(image_file) == len("Simulated JPEG content\n"):
        os.remove(image_file)
        print(f"Cleaned up dummy file: {image_file}")

view raw JSON →