This code is from https://www.blog.pythonlibrary.org, which is similar to others, which also fail in the same wayThis is the error code given
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
filename="screenshot.jpg"
def get_exif(image_file_path):
exif_table = {}
image = Image.open(image_file_path)
info = image.getexif()
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
exif_table[decoded] = value
print (f'Tag={tag}, Value={value}, decoded= {decoded}')
print (f'exif table is {exif_table}')
gps_info = {}
for key in exif_table['GPSInfo'].keys():
decode = GPSTAGS.get(key,key)
gps_info[decode] = exif_table['GPSInfo'][key]
return gps_info
if __name__ == "__main__":
exif = get_exif(filename)
print(exif)
The error message is:
Traceback (most recent call last):
for key in exif_table['GPSInfo'].keys(): AttributeError: 'int' object has no attribute 'keys'
I understand that the value for GPSInfo is itself a key into another part of the file which itself has keys and values regarding the latitude and longitude. In the case of this image file GPSInfo is 90. I assume that is a integer and that is what's causing the problem, but as the sample code appears in multiple places on the inter web, I can't see what the real problem is
I added some print lines right before the gps_info={} statement. This shows exactly what the line
for key in exif_table['GPSInfo'].keys():
is dealing with
Tag=34853, Value=90, decoded= GPSInfo Tag=296, Value=2, decoded= ResolutionUnit Tag=531, Value=1, decoded= YCbCrPositioning'Tag=282, Value=96.0, decoded= XResolution Tag=283, Value=96.0, decoded= YResolution exif table is {'GPSInfo': 90, 'ResolutionUnit': 2, 'YCbCrPositioning': 1, 'XResolution': 96.0, 'YResolution': 96.0}
The Tag 34853 correctly equates to GPSInfo. The value of GPSInfo in this file is 90.
I understand that at location 90 of the file, one would expect to find a string of values that represent key:value data for the GPS coordinates, hence the "for key in exif_table ['GPSInfo'].keys():"statement. I assume it fails because the value of exif_table["GPSInfo"] is 90 and seen as an integer, not a dictionary. It looks like a step is missing, but as its widely published code, I assume it must work and that it's me that has something wrong
Reading something similar and it can be worked around by using _getexit() instead of getexif() https://github.com/python-pillow/Pillow/issues/5863
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With