How to read the EXIF data for an image in Python?

Sometimes, we want to read the EXIF data for an image in Python.

In this article, we’ll look at how to read the EXIF data for an image in Python.

How to read the EXIF data for an image in Python?

To read the EXIF data for an image in Python, we can use the PIL.ExifTags module.

For instance, we write:

import PIL.ExifTags
import PIL.Image

img = PIL.Image.open('Canon_40D.jpg')
exif_data = img._getexif()

exif = {
    PIL.ExifTags.TAGS[k]: v
    for k, v in img._getexif().items() if k in PIL.ExifTags.TAGS
}
print(exif)

We open the image with:

img = PIL.Image.open('Canon_40D.jpg')

Then we get the EXIF data with:

exif_data = img._getexif()

Next, we get the EXIF tags and values from the EXIF data with:

exif = {
    PIL.ExifTags.TAGS[k]: v
    for k, v in img._getexif().items() if k in PIL.ExifTags.TAGS
}

As a result, we get something like:

{'GPSInfo': {0: b'x02x02x00x00'}, 'ResolutionUnit': 2, 'ExifOffset': 214, 'Make': 'Canon', 'Model': 'Canon EOS 40D', 'Software': 'GIMP 2.4.5', 'Orientation': 1, 'DateTime': '2008:07:31 10:38:11', 'YCbCrPositioning': 2, 'XResolution': 72.0, 'YResolution': 72.0, 'ExifVersion': b'0221', 'ComponentsConfiguration': b'x01x02x03x00', 'ShutterSpeedValue': 7.375, 'DateTimeOriginal': '2008:05:30 15:56:01', 'DateTimeDigitized': '2008:05:30 15:56:01', 'ApertureValue': 5.625, 'ExposureBiasValue': 0.0, 'MeteringMode': 5, 'UserComment': b'x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00', 'Flash': 9, 'FocalLength': 135.0, 'ColorSpace': 1, 'ExifImageWidth': 100, 'ExifInteroperabilityOffset': 948, 'FocalPlaneXResolution': 4438.356164383562, 'FocalPlaneYResolution': 4445.969125214408, 'SubsecTime': '00', 'SubsecTimeOriginal': '00', 'SubsecTimeDigitized': '00', 'ExifImageHeight': 68, 'FocalPlaneResolutionUnit': 2, 'ExposureTime': 0.00625, 'FNumber': 7.1, 'ExposureProgram': 1, 'CustomRendered': 0, 'ISOSpeedRatings': 100, 'ExposureMode': 1, 'FlashPixVersion': b'0100', 'WhiteBalance': 0, 'SceneCaptureType': 0}

for exif.

Conclusion

To read the EXIF data for an image in Python, we can use the PIL.ExifTags module.