Hack with Image: Extract Metadata from an Image using Python

extracting exif or metadata from an image using a python program

Introduction

Exif, which stands for Exchangeable image file format and, Exif data or Metadata is the information stored in your photo from your camera. Whenever you click a photo through your camera, some information is kept automatically in your raw image file or JPEG file.

Exif is quite interesting to find out how the person took the photo and much more information including GPS.

I should let you know that only JPEG files store Exif or Metadata of an image. So you should not try to find metadata from other types of image files, you'll get nothing interesting.

In this tutorial, we will try to Extract Metadata from Images using a Python Program. So, before moving on, let's check out what you need to install if you didn't already.

 ðŸ‘‰Visit Also: Add Your Watermark on a Photo using Python: Add Text & Logo

Requirements

There is a python imaging library called Pillow which offers a module, ExifTags to find out Metadata or Exif data from a photo.

Use pip3 instead of pip for Linux.

☛Install Pillow: pip install Pillow

The Image

Once I clicked this image(given below) through my mobile camera. Here, I will use that(sample.jpg) in the code to show the output to you. You can get it(the original file) through the Download button given below.

extracting metadata from this photo using a python program
sample.jpg

Import the modules

Recommending you, create a different directory("Get_Exifdata") for this task and declare a python file there with this name, "getExif.py". Now start writing your code by importing these modules.


from PIL import Image, ExifTags

Exclusion list

ExifTags returns some key values in human-unreadable format. You can ignore those keys from printing.

Here, we are declaring a list 'ex_list', containing several key names which will be ignored by the program later from printing. I excluded MakerNote, UserComment, and GPS Information(for security purpose) here.


# list for exclusion these key and
# corresponding value
ex_list = ['MakerNote', 'UserComment', 'GPSInfo']

Important Note
Try not to find out metadata from an image downloaded from WhatsApp; because normally WhatsApp removes Exif data from an original image while sending. Not only WhatsApp, but the same scenario also happens with the majority of web images.
So, I highly recommend you to use an original image file clicked by mobile or any kind of external camera, to get the desired results.


Open the image and get Exif

Now we will open an image using 'Image.open()' method and store the data into the 'img' variable. You have to pass the image path through it.

Next, 'img.getexif()' returns the exif or metadata(if exists) found from the image and store into 'exif_data' variable.


# Opening the image
img = Image.open("sample.jpg")
# Reading the metadata of that image
exit_data = img.getexif()

Check whether Exif data is present or not

In the previous state, we get the Exif data and stored it into a variable, 'exif_data'. Here, we will check if the data really exists or not by counting the length of that variable using the 'len()' method.


# It's true means, there is no meta or exif data
if len(exit_data) == 0:
print('Sorry, the image has no exif data.')

The metadata exists

This step implies that our program has found the metadata from the image. Since the data is in compact form, we have to separate keys and values using 'items()' method.

The next four lines are pretty straightforward; despite that, I have mentioned a single comment before each line of the code.


else:
for key, data in exit_data.items():
# chekcing if the key is present in the
# ExifTags.TAGS list
if key in ExifTags.TAGS:
# Getting the keyname from the id number
key_name = ExifTags.TAGS[key]
# Excluding some key names from printing
if key_name not in ex_list:
# Printing the Exif Data
print(f'{key_name}:{data}')

Output

I excluded GPS information(and two more keys) here for security purposes. Beyond that I hope you will find something interesting in this output.

ExifVersion:b'0221'
ComponentsConfiguration:b'\x01\x02\x03\x00'
ShutterSpeedValue:(62783, 15467)
DateTimeOriginal:2019:07:28 18:35:31
DateTimeDigitized:2019:07:28 18:35:31
ApertureValue:(193685, 85136)
BrightnessValue:(18051, 9380)
ExposureBiasValue:(66353, 33437)
MeteringMode:3
Flash:24
FocalLength:(83, 20)
ColorSpace:1
ExifImageWidth:4032
FocalLengthIn35mmFilm:29
SceneCaptureType:0
Make:Apple
ExifImageHeight:3024
SubsecTimeOriginal:658
SubsecTimeDigitized:658
Model:iPhone SE
SubjectLocation:(2125, 1660, 753, 756)
Orientation:1
YCbCrPositioning:1
SensingMethod:2
ExposureTime:(1, 17)
XResolution:(72, 1)
YResolution:(72, 1)
FNumber:(11, 5)
SceneType:b'\x01'
ExposureProgram:2
ISOSpeedRatings:250
ResolutionUnit:2
ExposureMode:0
FlashPixVersion:b'0100'
WhiteBalance:0
Software:12.3.1
LensSpecification:((83, 20), (83, 20), (11, 5), (11, 5))
LensMake:Apple
LensModel:iPhone SE back camera 4.15mm f/2.2
DateTime:2019:07:28 18:35:31
ExifOffset:194

Download the Image

If you're troubling to find an original image for working with, you can download the image I used in the program from my GitHub page(https://github.com/subhankar-rakshit) through the Download button.

Summary

In this tutorial, we learned how to extract or get metadata from an image using python. We build this program step-by-step. You just need to follow all of those explained above.

Images downloaded from the web(maximum time) or WhatsApp don't store any Exif data. So if you want to get the result properly, use an original image clicked by a camera directly(without modification).

For any queries related to this topic, please leave your comment below. You'll get an immediate response.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post