How to extract all exif information with Pillow

Pillow is a Python Imaging Library that provides support for opening, manipulating, and saving many different image file formats. If you want to extract all Exif (Exchangeable image file format) information from an image using Pillow, you can use the following code:

from PIL import Image

def extract_exif_info(image_path):
    try:
        # Open the image file
        with Image.open(image_path) as img:
            # Check if the image has Exif data
            if hasattr(img, '_getexif'):
                exif_info = img._getexif()
                if exif_info is not None:
                    # Print all Exif information
                    for tag, value in exif_info.items():
                        print(f"Tag: {tag}, Value: {value}")
                else:
                    print("No Exif information found in the image.")
            else:
                print("Exif information is not supported for this image format.")
    except Exception as e:
        print(f"An error occurred: {e}")

# Example usage:
image_path = "path/to/your/image.jpg"
extract_exif_info(image_path)

This code defines a function extract_exif_info that takes the path to an image file as an argument. Inside the function, it opens the image using Pillow’s Image.open method and checks if the image has Exif data. If Exif data is present, it prints out all the Exif tags and their corresponding values.

Make sure to replace "path/to/your/image.jpg" with the actual path to your image file.

Note: Keep in mind that not all image formats or images may have Exif information, and the availability of Exif data depends on how the image was created or modified.

Leave a Reply

Your email address will not be published. Required fields are marked *