The Python Oracle

In Python, how do I read the exif data for an image?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World

--

Chapters
00:00 Question
00:20 Accepted answer (Score 228)
00:50 Answer 2 (Score 55)
02:29 Answer 3 (Score 44)
02:44 Answer 4 (Score 21)
03:10 Thank you

--

Full question
https://stackoverflow.com/questions/4764...

Answer 1 links:
[Pillow 6.0.0 release notes]: https://pillow.readthedocs.io/en/stable/...
[ExifTags.TAGS]: https://pillow.readthedocs.io/en/stable/...

Answer 2 links:
[ExifRead]: https://pypi.python.org/pypi/ExifRead

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #image #pythonimaginglibrary #exif

#avk47



ACCEPTED ANSWER

Score 238


You can use the _getexif() protected method of a PIL Image.

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

This should give you a dictionary indexed by EXIF numeric tags. If you want the dictionary indexed by the actual EXIF tag name strings, try something like:

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



ANSWER 2

Score 71


For Python 3.x and using Pillow 6.x and above, Image objects now provide a "public"/official getexif() method that returns a <class 'PIL.Image.Exif'> instance or None if the image has no EXIF data.

From Pillow 6.0.0 release notes:

getexif() has been added, which returns an Exif instance. Values can be retrieved and set like a dictionary. When saving JPEG, PNG or WEBP, the instance can be passed as an exif argument to include any changes in the output image.

As stated, you can iterate over the key-value pairs of the Exif instance like a regular dictionary. The keys are 16-bit integers that can be mapped to their string names using the ExifTags.TAGS module.

from PIL import Image, ExifTags

img = Image.open("sample.jpg")
img_exif = img.getexif()
print(type(img_exif))
# <class 'PIL.Image.Exif'>

if img_exif is None:
    print('Sorry, image has no exif data.')
else:
    for key, val in img_exif.items():
        if key in ExifTags.TAGS:
            print(f'{ExifTags.TAGS[key]}:{val}')
        else:
            print(f'{key}:{val}')

For EXIF keys that appear in the ExifTags.TAGS module, you should see something like this:

ExifVersion:b'0230'
...
FocalLength:(2300, 100)
ColorSpace:1
...
Model:'X-T2'
Make:'FUJIFILM'
LensSpecification:(18.0, 55.0, 2.8, 4.0)
...
DateTime:'2019:12:01 21:30:07'
...

Tested with Python 3.8.8 and Pillow 8.1.0.




ANSWER 3

Score 46


You can also use the ExifRead module:

import exifread
# Open image file for reading (binary mode)
f = open(path_name, 'rb')

# Return Exif tags
tags = exifread.process_file(f)



ANSWER 4

Score 20


I use this:

import os,sys
from PIL import Image
from PIL.ExifTags import TAGS

for (k,v) in Image.open(sys.argv[1])._getexif().items():
        print('%s = %s' % (TAGS.get(k), v))

or to get a specific field:

def get_field (exif,field) :
  for (k,v) in exif.items():
     if TAGS.get(k) == field:
        return v

exif = image._getexif()
print get_field(exif,'ExposureTime')