Piexif

1.1.3 · active · verified Sat Apr 11

Piexif is a pure Python library designed to simplify EXIF data manipulation. It enables reading, writing, and removing EXIF tags from JPEG, WebP, and TIFF image files without relying on external image processing libraries. The library's current version is 1.1.3, and releases are made as needed to address issues or add features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load EXIF data from a JPEG image, modify a tag (e.g., the camera make), and then save the image with the updated EXIF information using piexif, often in conjunction with Pillow for image handling.

import piexif
from PIL import Image
import os

# Create a dummy JPEG file for demonstration
dummy_image_path = "dummy_image_with_exif.jpg"
new_image_path = "output_image_with_modified_exif.jpg"

# Create a simple image (requires Pillow)
img = Image.new('RGB', (60, 30), color = 'red')
img.save(dummy_image_path)

# 1. Load EXIF data
try:
    # Create a minimal EXIF dictionary
    zeroth_ifd = {
        piexif.ImageIFD.Make: "PiexifTest",
        piexif.ImageIFD.XResolution: (72, 1),
        piexif.ImageIFD.YResolution: (72, 1)
    }
    exif_ifd = {
        piexif.ExifIFD.DateTimeOriginal: "2026:04:11 12:34:56"
    }
    exif_dict_initial = {"0th": zeroth_ifd, "Exif": exif_ifd, "GPS": {}, "Interop": {}, "1st": {}, "thumbnail": None}
    exif_bytes_initial = piexif.dump(exif_dict_initial)

    img.save(dummy_image_path, exif=exif_bytes_initial)

    exif_dict = piexif.load(dummy_image_path)
    print("Original Camera Make:", exif_dict["0th"][piexif.ImageIFD.Make])

    # 2. Modify an EXIF tag
    exif_dict["0th"][piexif.ImageIFD.Make] = "PiexifModified"

    # 3. Dump the modified EXIF data to bytes
    exif_bytes = piexif.dump(exif_dict)

    # 4. Insert the new EXIF data into an image (or save with Pillow)
    img_to_modify = Image.open(dummy_image_path)
    img_to_modify.save(new_image_path, exif=exif_bytes)

    # 5. Verify the change
    modified_exif_dict = piexif.load(new_image_path)
    print("Modified Camera Make:", modified_exif_dict["0th"][piexif.ImageIFD.Make])

finally:
    # Clean up dummy files
    if os.path.exists(dummy_image_path):
        os.remove(dummy_image_path)
    if os.path.exists(new_image_path):
        os.remove(new_image_path)

view raw JSON →