Reputation: 419
I am trying to read the metadata of my images using Pillow's TAGS
functionality. I use the following code to get this information:
# imports
from PIL import Image
from PIL.ExifTags import TAGS
imagename = 'image.jpg'
image = Image.open(imagename)
exifdata = image.getexif()
for tag_id in exifdata:
tag = TAGS.get(tag_id,tag_id)
print(tag_id)
data = exifdata.get(tag_id)
# we decode bytes
if isinstance(data,bytes):
data = data.decode()
print(f'{tag:25}: {data}')
The result I get is similar to the following output:
ImageWidth : 4128
ImageLength : 1908
ResolutionUnit : 2
ExifOffset : 226
Make : samsung
Model : SM-M205F
Software : M205FDDS8CUC1
Orientation : 8
DateTime : 2021:06:18 12:08:37
YCbCrPositioning : 1
XResolution : 72.0
YResolution : 72.0
The problem is that critical data, that I need, is not in the list. For example focal length, metering mode, exposure time, and other metrics and details are not there, though I can see them by right clicking my image in the "Properties" section as the following image shows:
Why Pillow's TAGS
can't read all the details. Is there any way to make it get all the details needed?
Upvotes: 3
Views: 6123
Reputation: 1
If you wanna read/modify an image's EXIF, another alternative/recommended module is pyexiv2 (https://github.com/LeoHsiao1/pyexiv2)
>>> import pyexiv2
>>> img = pyexiv2.Image(r'.\pyexiv2\tests\1.jpg')
>>> data = img.read_exif()
>>> print(data)
>>> img.close()
click the url above, read tutorial for more information.
Upvotes: 0
Reputation: 1039
The properties that you've mentioned - focal length, metering mode and exposure time - are within the EXIF IFD - https://www.awaresystems.be/imaging/tiff/tifftags/privateifd/exif.html
Since Pillow 8.2.0, getexif()
only returns the top level tags. To get the data within the EXIF IFD, you need to use get_ifd()
.
To take some code from the other answer here and modify it,
from PIL import Image
from PIL.ExifTags import TAGS
def print_exif_data(exif_data):
for tag_id in exif_data:
tag = TAGS.get(tag_id, tag_id)
content = exif_data.get(tag_id)
print(f'{tag:25}: {content}')
with Image.open("Tests/images/flower.jpg") as im:
exif = im.getexif()
print_exif_data(exif)
print()
print_exif_data(exif.get_ifd(0x8769))
I get
ResolutionUnit : 2
ExifOffset : 196
Make : Canon
Model : Canon PowerShot S40
Orientation : 1
DateTime : 2003:12:14 12:01:44
YCbCrPositioning : 1
XResolution : 180.0
YResolution : 180.0
ExifVersion : b'0220'
ComponentsConfiguration : b'\x01\x02\x03\x00'
CompressedBitsPerPixel : 5.0
DateTimeOriginal : 2003:12:14 12:01:44
DateTimeDigitized : 2003:12:14 12:01:44
ShutterSpeedValue : 8.96875
ApertureValue : 4.65625
ExposureBiasValue : 0.0
MaxApertureValue : 2.970855712890625
MeteringMode : 2
Flash : 24
FocalLength : 21.3125
UserComment : b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
ColorSpace : 1
ExifImageWidth : 2272
FocalPlaneXResolution : 8114.285714285715
ExifImageHeight : 1704
FocalPlaneYResolution : 8114.285714285715
FocalPlaneResolutionUnit : 2
SensingMethod : 2
FileSource : b'\x03'
ExposureTime : 0.002
ExifInteroperabilityOffset: 1416
FNumber : 4.9
CustomRendered : 0
ExposureMode : 0
FlashPixVersion : b'0100'
WhiteBalance : 0
DigitalZoomRatio : 1.0
MakerNote : b'\x0c\x00\x01\x00\x03\x00(\x00\x00\x00D\x04\x00\x00\x02\x00\x03\x00\x04\x00\x00\x00\x94\x04\x00\x00\x03\x00\x03\x00\x04\x00\x00\x00\x9c\x04\x00\x00\x04\x00\x03\x00\x1b\x00\x00\x00\xa4\x04\x00\x00\x00\x00\x03\x00\x06\x00\x00\x00\xda\x04\x00\x00\x00\x00\x03\x00\x04\x00\x00\x00\xe6\x04\x00\x00\x06\x00\x02\x00 \x00\x00\x00\xee\x04\x00\x00\x07\x00\x02\x00\x18\x00\x00\x00\x0e\x05\x00\x00\x08\x00\x04\x00\x01\x00\x00\x00;\xe1\x11\x00\t\x00\x02\x00 \x00\x00\x00&\x05\x00\x00\x10\x00\x04\x00\x01\x00\x00\x00\x00\x00\x11\x01\r\x00\x03\x00\x15\x00\x00\x00F\x05\x00\x00\x00\x00\x00\x00P\x00\x02\x00\x00\x00\x05\x00\x01\x00\x00\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x05\x00\x01\x00\x030\x01\x00\xff\xff\xff\xff\xaa\x02\xe3\x00 \x00\x95\x00\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff1\x00\xe0\x08\xe0\x08\x00\x00\x01\x00\x02\x00\xaa\x02\x1e\x01\xd7\x00\x00\x00\x00\x00\x00\x00\x00\x006\x00\x00\x00\xa0\x00\x14\x01\x95\x00\x1f\x01\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x020\x00\x00\x00\x00\x00\x00\x01\x00\x0e\x03\x00\x00\x95\x00!\x01\x00\x00\x00\x00\x00\x00\xfa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00IMG:PowerShot S40 JPEG\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Firmware Version 1.10\x00\x00\x00Andreas Huggel\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00*\x00\x03\x00\x01\x80z\x01\x01\x80\x00\x00\x00\x00\x00\x00\x03\x01\x02\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x009\x00\xc6\x00\x05\x00\x00\x00\x00\x00\x00\x00'
SceneCaptureType : 0
Upvotes: 6
Reputation: 18895
Instead of the "official" Image.getexif
method, you can use the older, not really documented Image._getexif
method (attention, that's a protected member of the Image
class):
from PIL import Image
from PIL.ExifTags import TAGS
def print_exif_data(exif_data):
for tag_id in exif_data:
tag = TAGS.get(tag_id, tag_id)
content = exif_data.get(tag_id)
if isinstance(content, bytes):
content = content.decode()
print(f'{tag:25}: {content}')
print()
# https://github.com/ianare/exif-samples/blob/master/jpg/mobile/jolla.jpg
imagename = 'jolla.jpg'
image = Image.open(imagename)
print_exif_data(image.getexif())
# ExifOffset : 146
# Make : Jolla
# Model : Jolla
# Orientation : 1
# DateTime : 2014:09:21 16:00:56
# XResolution : 72.0
# YResolution : 72.0
print_exif_data(image._getexif())
# Model : Jolla
# Orientation : 1
# DateTime : 2014:09:21 16:00:56
# ExifOffset : 146
# XResolution : 72.0
# YResolution : 72.0
# Make : Jolla
# ExifVersion : 0230
# ShutterSpeedValue : 4.643856189774724
# ApertureValue : 2.526068811667588
# DateTimeOriginal : 2014:09:21 16:00:56
# ExposureBiasValue : 0.0
# FlashPixVersion : 0100
# WhiteBalance : 1
# ISOSpeedRatings : 320
# MeteringMode : 1
# Flash : 0
# FocalLength : 4.0
# 34864: 3
# 34867: 320
# ExposureTime : 0.04
# FNumber : 2.4
At least for the linked test image, focal length, exposure time, etc. are properly extracted.
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.19041-SP0
Python: 3.9.1
PyCharm: 2021.1.1
Pillow: 8.2.0
----------------------------------------
Upvotes: 1