Open In App
Related Articles

How to extract image metadata in Python?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

Using Exif to extract image metadata

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:

  • Importing modules.
  • Load the image and extract the exif data.
  • 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:

 

Using subprocess to extract image metadata

Here we are using the subprocess module to extract image metadata using popen() method which opens a pipe from a command. And this pipe allows the command to send its output to another command. And we are storing the fetched metadata into a dictionary.

Python3

import subprocess
 
imgPath = "C:\\Users\\DELL\\Downloads\\output.jpg"
exeProcess = "hachoir-metadata"
process = subprocess.Popen([exeProcess,imgPath],
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT,
                           universal_newlines=True)
Dic={}
 
for tag in process.stdout:
        line = tag.strip().split(':')
        Dic[line[0].strip()] = line[-1].strip()
 
for k,v in Dic.items():
    print(k,':', v)

                    

Output:

 



Last Updated : 13 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads