Basic Image Operations

Python Pillow Color Conversions

Image Manipulation

Image Filtering

Image Enhancement and Correction

Image Analysis

Advanced Topics

  • Image Module
  • Python Pillow Useful Resources

    Python Pillow - Extracting Image Metadata



    Image metadata refers to information associated with a digital image. This metadata can include various details, such as camera model, date and time of capture, location information (GPS coordinates), and keywords.

    In this context, extracting image metadata retrieves this underlying information from an image. One widely used type of image metadata is EXIF data, short for "Exchangeable Image File Format." This standardized format, established by organizations like Canon, Minolta/Sony, and Nikon, encapsulates a comprehensive set of specifics related to the image, including camera settings, exposure parameters, and more.

    It's important to note that all EXIF data is metadata, but not all metadata is EXIF data. EXIF is a specific type of metadata, and the data you're seeking might also fall under other metadata types, such as IPTC or XMP data.

    Example - Extracting Basic Image Metadata

    The Python Pillow Image object offers a straightforward way of accessing the basic image metadata, including the filename, dimensions, format, mode, and more.

    The example demonstrates how to extract various basic metadata from an image using attributes of the Pillow Image object.

    main.py

    from PIL import Image
    
    # Path to the image
    image_path = "Images/dance-cartoon.gif"
    
    # Read the image data using Pillow
    image = Image.open(image_path)
    
    # Access and print basic image metadata
    print("Filename:", image.filename)
    print("Image Size:", image.size)
    print("Image Height:", image.height)
    print("Image Width:", image.width)
    print("Image Format:", image.format)
    print("Image Mode:", image.mode)
    
    # Check if the image is animated (for GIF images)
    if hasattr(image, "is_animated"):
       print("Image is Animated:", image.is_animated)
       print("Frames in Image:", image.n_frames)
    

    Output

    Filename: Images/dance-cartoon.gif
    Image Size: (370, 300)
    Image Height: 300
    Image Width: 370
    Image Format: GIF
    Image Mode: P
    Image is Animated: True
    Frames in Image: 12
    

    Extracting Advanced Image Metadata

    The Python Pillow library offers tools to access and manage image metadata, specifically through the Image.getexif() function and the ExifTags module.

    The Image.getexif() function retrieves EXIF data from an image, which includes a wealth of valuable information about the image. The syntax for using this function is −

    Image.getexif()
    

    The function returns an Exif object, This object provides read and write access to EXIF image data.

    The PIL.ExifTags.TAGS dictionary is used to interpret EXIF data. This dictionary maps 16-bit integer EXIF tag enumerations to human-readable descriptive string names, making the metadata easier to understand. The syntax for this dictionary is −

    PIL.ExifTags.TAGS: dict
    

    Example - Getting EXIF metadata

    The following example demonstrates how to access and print EXIF metadata from an image file using Pillow's getexif() method. It also shows how to use the TAGS dictionary to map tag IDs to human-readable tag names.

    main.py

    from PIL import Image
    from PIL.ExifTags import TAGS
    
    # The path to the image 
    image_path = "Images/flowers_canon.JPG"
    
    # Open the image using the PIL library
    image = Image.open(image_path)
    
    # Extract EXIF data
    exif_data = image.getexif()
    
    # Iterate over all EXIF data fields
    for tag_id, data in exif_data.items():
        
       # Get the tag name, instead of the tag ID
       tag_name = TAGS.get(tag_id, tag_id)
       print(f"{tag_name:25}: {data}")
    

    Output

    GPSInfo                  : 10628
    ResolutionUnit           : 2
    ExifOffset               : 360
    Make                     : Canon
    Model                    : Canon EOS 80D
    YResolution              : 72.0
    Orientation              : 8
    DateTime                 : 2020:10:25 15:39:08
    YCbCrPositioning         : 2
    Copyright                : CAMERAMAN_SAI
    XResolution              : 72.0
    Artist                   : CAMERAMAN_SAI
    
    Advertisements