DEV Community

Cover image for Read Exif tags with Python

Posted on

Read Exif tags with Python

Images sometimes contain Exif tags. Many photos store date and geolocation in these tags. Did you know you can read them in the Python programming language?

Of course, you need to know Python programming.

But once you know that, there's a module to read the Exif tags.

Yes, there's a module for that!

pip install exifread

The program then has a few steps. Load the image, grab the tags then output them. For simplicity I've put it in a function.

import exifread

def process_img(path):
    f = open(path, 'rb')
    tags = exifread.process_file(f)
    info = {
        'Image DateTime': tags.get('Image DateTime', '0'),
        'GPS GPSLatitudeRef': tags.get('GPS GPSLatitudeRef', '0'),
        'GPS GPSLatitude': tags.get('GPS GPSLatitude', '0'),
        'GPS GPSLongitudeRef': tags.get('GPS GPSLongitudeRef', '0'),
        'GPS GPSLongitude': tags.get('GPS GPSLongitude', '0')
    return info

info = process_img('image.jpg')

Example output for the program:

{'Image DateTime': (0x0132) ASCII=2002:11:18 22:46:09 @ 218, 
 'GPS GPSLatitudeRef': '0', 
 'GPS GPSLatitude': '0', 
 'GPS GPSLongitudeRef': '0', 
 'GPS GPSLongitude': '0'}

If the GPS data is not stored in the image, it will show 0 as value. So to test it, you need images with gps data.

You can find some test images here.

However, if contains GPS data (many mobile phones have GPS). It could return something like this:

{'Image DateTime': (0x0132) ASCII=2008:11:01 21:15:07 @ 248, 
 'GPS GPSLatitudeRef': (0x0001) ASCII=N @ 936, 
 'GPS GPSLatitude': (0x0002) Ratio=[43, 28, 1407/500] @ 1052, 
 'GPS GPSLongitudeRef': (0x0003) ASCII=E @ 960, 
 'GPS GPSLongitude': (0x0004) Ratio=[11, 53, 645599999/100000000] @ 1076}

Related links:

Top comments (1)

diana75082290 profile image

Good article. I will use the information. Thanks.