Lake_Lagunita
Lake_Lagunita

Reputation: 553

Python PIL Exif_data 'IFDRational' object does not support indexing

I am running an example of reconstruct 3D object from images from here .

When I tries to get the focal length of the picture as the article shows:

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

#Get focal length in tuple form
focal_length_exif = exif_data['FocalLength']

#Get focal length in decimal form
focal_length = focal_length_exif[0]/focal_length_exif[1]

My code reports an error saying "'IFDRational' object does not support indexing" and this focal_length_exif has a structure like this: enter image description here should I use focal_length = focal_length_exif.real which is 399/100? (I am not very clear about the format of focal length)

Upvotes: 1

Views: 547

Answers (1)

ePuntel
ePuntel

Reputation: 191

The focal length can be recovered from a PIL image object with key 37386 from _getexif() dictionary items, as in the code...

imafl = exif_img._getexif()[37386]

imafl, defined above, is a PIL.TiffImagePlugin.IFDRational, same type as your focal_length_exif, and also can not be indexed, just like an int or a float types can not be indexed. IFDRationals has the following attributes: ['conjugate', 'denominator', 'imag', 'numerator', 'real']. If you want to perform math operations with an IFDRational, I'd suggest converting it to float:

fimafl = float(imafl)

IFDRational.real attribute allows something like 399/0, or else 0/100 and it looks like that's why PIL uses this type.

Upvotes: 2

Related Questions