Skip to content
Related Articles

Related Articles

How to extract image metadata in Python?
  • Last Updated : 03 Jan, 2021

Prerequisites: PIL

Metadata stands for data about data. In case of images, metadata means details about the image and its production. Some metadata is generated automatically by the capturing device. 

Some details contained by image metadata is as follows:

  • Height
  • Width
  • Date and Time
  • Model etc.

Python has PIL library which makes the task of extracting metadata from an image extremely easy that too by using just a few lines.

Approach:

  • Import the pillow module.
  • Load the image
  • Get the metadata. The metadata so obtained
  • Convert it into human-readable form

There are many types of metadata but in this we are only focusing on Exif metadata



Exif

These metadata, often created by cameras and other capture devices, include technical information about an image and its capture method, such as exposure settings, capture time, GPS location information and camera model.

for this i have used this picture(link for this is below) make sure whatever picture you are using it  should have some metadata of EXIF type and most of the capturing devices have exif data

https://drive.google.com/file/d/1z2RaRvzC8Cd8oxD8pZJu2U0rpRr8tbwV/view 

NOTE:-please avoid the image shared through whatsapp. Whatsapp strips away all metadata from any image 

Implementation:

Step 1: Importing modules.

Step 2: Load the image and extract the exif data.



Step 3: Convert the exif tag id(denoted by tagid in the code ) into human readable form denoted by tagname in the code and getting its respective value.

Application to extract  Metadata from a image with pillow : below script implements the above approach

Python3




from PIL import Image
from PIL.ExifTags import TAGS
  
# open the image
image = Image.open("img.jpg")
  
# extracting the exif metadata
exifdata = image.getexif()
  
# looping through all the tags present in exifdata
for tagid in exifdata:
      
    # getting the tag name instead of tag id
    tagname = TAGS.get(tagid, tagid)
  
    # passing the tagid to get its respective value
    value = exifdata.get(tagid)
    
    # printing the final result
    print(f"{tagname:25}: {value}")

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :