Read Exif tags with Python

petercour - Jul 23 '19 - - Dev Community

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.

#!/usr/bin/python3
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')
print(info)

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:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player