Abdeladim Fadheli · 4 min read · Updated jul 2022 · Ethical Hacking · Web Scraping
Confused by complex code? Let our AI-powered Code Explainer demystify it for you. Try it out!
Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.
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.
Please note that there are free tools to extract metadata such as ImageMagick or ExifTool on Linux, the goal of this tutorial is to extract metadata with the Python programming language.
Get -35 OFF Now: Ethical Hacking with Python EBook
To get started, you need to install Pillow library:
pip3 install Pillow
Open up a new Python file and follow along:
from PIL import Imagefrom 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 videoimagename = "image.jpg"# read the image data using PILimage = Image.open(imagename)
We loaded the image using the Image.open()
method. Before calling the getexif()
function, the Pillow library has some attributes on the image object, let's print them out:
# extract other basic metadatainfo_dict = { "Filename": image.filename, "Image Size": image.size, "Image Height": image.height, "Image Width": image.width, "Image Format": image.format, "Image Mode": image.mode, "Image is Animated": getattr(image, "is_animated", False), "Frames in Image": getattr(image, "n_frames", 1)}for label,value in info_dict.items(): print(f"{label:25}: {value}")
Get Now: Ethical Hacking with Python EBook
Now let's call the getexif()
method on the image which returns image metadata:
# extract EXIF dataexifdata = 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 fieldsfor 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:
Filename : .\image.jpgImage Size : (5312, 2988) Image Height : 2988Image Width : 5312Image Format : JPEGImage Mode : RGBImage is Animated : FalseFrames in Image : 1ExifVersion : 0220ShutterSpeedValue : 4.32ApertureValue : 1.85DateTimeOriginal : 2016:11:10 19:33:22DateTimeDigitized : 2016:11:10 19:33:22BrightnessValue : -1.57ExposureBiasValue : 0.0MaxApertureValue : 1.85MeteringMode : 3Flash : 0FocalLength : 4.3ColorSpace : 1ExifImageWidth : 5312FocalLengthIn35mmFilm : 28SceneCaptureType : 0ImageWidth : 5312ExifImageHeight : 2988ImageLength : 2988Make : samsungModel : SM-G920FOrientation : 1YCbCrPositioning : 1XResolution : 72.0YResolution : 72.0ImageUniqueID : A16LLIC08SM A16LLIL02GMExposureProgram : 2ISOSpeedRatings : 640ResolutionUnit : 2ExposureMode : 0FlashPixVersion : 0100WhiteBalance : 0Software : G920FXXS4DPI4DateTime : 2016:11:10 19:33:22ExifOffset : 226MakerNote : 0100 Z@PUserComment :ExposureTime : 0.05FNumber : 1.9
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!
Related: How to Extract Video Metadata in Python
Want to Learn More?
If you're a beginner and want to learn Python, I suggest you take the Python For Everybody Coursera course, in which you'll learn a lot about Python. You can also check ourresources and courses pageto see the Python resources I recommend!
Finally, we have an EBook that is for ethical hackers like you, where we build 24 hacking tools with Python from scratch! Make sure tocheck it out here.
Learn also:How to Use Steganography to Hide Secret Data in Images in Python.
Happy Coding ♥
Just finished the article? Now, boost your next project with our Python Code Generator. Discover a faster, smarter way to code.
View Full Code Generate Python Code
Sharing is caring!
Read Also
Visit →
Visit →
Visit →
Comment panel
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!