How to access JPEG image metadata

Published on Aug. 22, 2023, 12:15 p.m.

To access JPEG image metadata, including EXIF data, in Python, you can use the Pillow library, which is a fork of the popular PIL (Python Imaging Library) library. Here’s an example of how to use Pillow to access the metadata of a JPEG image:

from PIL import Image

# Open the image
image = Image.open("example.jpg") # replace "example.jpg" with the name of your JPEG file

# Get the metadata
metadata = image.getexif()

# Print the metadata
for tag, value in metadata.items():
    print(f"Tag: {tag} \t Value: {value}")

In this example, we first open the JPEG image using Pillow‘s Image.open() method. Next, we call the getexif() method of the Image object to get the EXIF data as a dictionary. Finally, we loop through the dictionary to print the metadata items.

Note that the metadata returned by getexif() is in the form of tag-value pairs, where each tag is represented by an integer ID. You can look up the names of these tags using the ExifTags module in the Pillow library.