How to Extract Image Metadata in Python

Learn how you can extract image metadata such as GPS info, camera make, model and much more using Exchangeable Image file Format (EXIF) in Python with Pillow library.
  · 4 min read · Updated oct 2021 · Ethical Hacking · Web Scraping

In this tutorial, you will learn how you can extract some useful metadata within images using the Pillow library in Python.

Devices such as digital cameras, smartphones, and scanners use the EXIF standard to save images or audio files. This standard contains many useful tags to extract which can be useful for forensic investigation, such as the make, model of the device, the exact date and time of image creation, and even the GPS information on some devices.

Related: How to Extract PDF Metadata in Python

To get started, you need to install Pillow library:

pip3 install Pillow

Open up a new Python file and follow along:

from PIL import Image
from PIL.ExifTags import TAGS

Now this will only work on JPEG image files, take any image you took and test it for this tutorial (if you want to test on my image, you'll find it in the tutorial's repository):

# path to the image or video
imagename = "image.jpg"

# read the image data using PIL
image =

After reading the image using the function, let's call the getexif() method on the image which returns image metadata:

# extract EXIF data
exifdata = image.getexif()

The problem with exifdata variable now is that the field names are just IDs, not a human-readable field name, that's why we gonna need the TAGS dictionary from PIL.ExifTags module which maps each tag ID into a human-readable text:

# iterating over all EXIF data fields
for tag_id in exifdata:
    # get the tag name, instead of human unreadable tag id
    tag = TAGS.get(tag_id, tag_id)
    data = exifdata.get(tag_id)
    # decode bytes 
    if isinstance(data, bytes):
        data = data.decode()
    print(f"{tag:25}: {data}")

Here is my output:

ExifVersion              : 0220
ShutterSpeedValue        : (406, 100)
ApertureValue            : (1851, 1000)       
DateTimeOriginal         : 2016:11:10 18:22:47
DateTimeDigitized        : 2016:11:10 18:22:47
BrightnessValue          : (-109, 100)        
ExposureBiasValue        : (0, 10)
MaxApertureValue         : (1851, 1000)       
MeteringMode             : 2
Flash                    : 0
FocalLength              : (220, 100)
UserComment              :
ColorSpace               : 1
ExifImageWidth           : 2592
FocalLengthIn35mmFilm    : 22
SceneCaptureType         : 0
ExifImageHeight          : 1944
ImageWidth               : 2592
ImageLength              : 1944
Make                     : samsung
Model                    : SM-G920F
Orientation              : 8
YCbCrPositioning         : 1
ExposureTime             : (1, 17)
XResolution              : (72, 1)
YResolution              : (72, 1)
FNumber                  : (19, 10)
FNumber                  : (19, 10)
ImageUniqueID            : B05LLHA01PM
ISOSpeedRatings          : 400
ISOSpeedRatings          : 400
ISOSpeedRatings          : 400
ISOSpeedRatings          : 400
ResolutionUnit           : 2
ExifOffset               : 226
ExposureMode             : 0
FlashPixVersion          : 0100
WhiteBalance             : 0
Software                 : G920FXXS4DPI4
DateTime                 : 2016:11:10 18:22:4
MakerNote                :       0100                      Z   @         P          
                                                           Z   @         P

A bunch of useful stuff, by quickly googling the Model, I concluded that this image was taken by a Samsung Galaxy S6, run this on images that were captured by other devices and you'll see different (maybe more) fields.

Alright, we're done. A good challenge for you is to download all images from a URL and then run this tutorial's script on every image you find and investigate the interesting results!

Want to Learn More?

Finally, many of the Python concepts aren't discussed in detail here, if you feel you want to dig more into Python, I suggest you get one of these courses:

Learn also: How to Use Steganography to Hide Secret Data in Images in Python.

Happy Coding ♥

View Full Code
Sharing is caring!

Read Also

Comment panel