Juggling between coding languages? Let our Code Converter help. Your one-stop solution for language conversion. Start now!
In this tutorial, you will learn how you can extract some useful metadata within images using the Pillow library in Python.
Devices like 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 and 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.
Related: How to Extract Video Metadata in Python.
To get started, you need to install the Pillow library:
Open up a new Python file and follow along:
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):
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:
Get our Ethical Hacking with Python EBook
Now, let's call the getexif()
method on the image which returns image metadata:
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:
Here is my output:
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!
Finally, we have an EBook for ethical hackers like you, where we build 35+ hacking tools with Python from scratch! Check it out here.
Learn also: How to Use Steganography to Hide Secret Data in Images in Python.
Happy Coding ♥
Why juggle between languages when you can convert? Check out our Code Converter. Try it out today!
View Full Code Understand My Code
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!