In Python, how do I read the exif data for an image?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Melt
--
Chapters
00:00 In Python, How Do I Read The Exif Data For An Image?
00:14 Accepted Answer Score 238
00:39 Answer 2 Score 46
00:51 Answer 3 Score 20
01:07 Answer 4 Score 71
02:21 Thank you
--
Full question
https://stackoverflow.com/questions/4764...
--
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 anExifinstance. Values can be retrieved and set like a dictionary. When saving JPEG, PNG or WEBP, the instance can be passed as anexifargument 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')