Python Pillow: Read Image Metadata Information From an Image

In this tutorial, we will introduce how to get an image metadata information using python pillow library.

Python Pillow - Read Image Metadata Information From an Image - A Step Guide

1.Install pillow and prettytable library

pip install prettytable
pip install pillow

2.Import libraries

from PIL import Image
from PIL.ExifTags import TAGS
from prettytable import PrettyTable

3.Open an image

image_filename = "image.jpg"
my_image = Image.open(image_filename)

4.Get image metadata information

#get EXIF standard Data of the image
img_exif_data = my_image.getexif()

5.Format image metadata and output

#initialiting prettytable object
table = PrettyTable()

#setting table feilds name
table.field_names = ["MetaTags", "Values"]

for id  in img_exif_data:
    tag_name = TAGS.get(id, id)
    data = img_exif_data.get(id)
    
    #if data in bytes
    if isinstance(data, bytes):
        data = data.decode()
    #add tag name and data into table
    table.add_row([tag_name, data])

#display data
print(table)

Run this code, you may see these image metadata:

Python Pillow - Read Image Metadata Information From an Image