Movatterモバイル変換


[0]ホーム

URL:


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.
  · 3 min read · Updated sep 2025 ·Ethical Hacking ·Web Scraping ·Digital Forensics

Juggling between coding languages? Let ourCode 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 thePillow library in Python.

Devices like digital cameras, smartphones, and scanners use theEXIF 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:

$ 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 onJPEG 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 thetutorial's repository):

# path to the image or videoimagename = "image.jpg"# read the image data using PILimage = Image.open(imagename)

We loaded the image using theImage.open() method. Before calling thegetexif() 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 our Ethical Hacking with Python EBook

Now, let's call thegetexif() method on the image which returns image metadata:

# extract EXIF dataexifdata = image.getexif()

The problem withexifdata variable now is that the field names are just IDs, not a human-readable field name, that's why we gonna need theTAGS dictionary fromPIL.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 todownload 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 ♥

Want to code smarter? OurPython Code Assistant is waiting to help you. Try it now!

View Full Code Create Code for Me
Sharing is caring!



Read Also


Steganography: How to Hide Data in Images in Python
How to Make a Barcode Reader in Python
How to Extract Chrome Passwords in Python

Comment panel

    Got a coding query or need some guidance before you comment? Check out thisPython Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!





    Ethical Hacking with Python EBook - Topic - Top


    Join 50,000+ Python Programmers & Enthusiasts like you!



    Tags

    Ethical Hacking with Python EBook - Topic - Middle


    New Tutorials

    Popular Tutorials


    Ethical Hacking with Python EBook - Topic - Bottom

    CodingFleet - Topic - Bottom






    Claim your Free Chapter!

    Download a Completely Free Ethical hacking with Python from Scratch Chapter.

    See how the book can help you build awesome hacking tools with Python!



    [8]ページ先頭

    ©2009-2025 Movatter.jp