Andrei
Andrei

Reputation: 33

Python3 Exif orientation KeyError: '274'

I'm trying to pull the Exif orientation from an image, but for some reason I keep getting KeyError: '274', even though it seems to be in the dictionary with a value of '8'.

Snippet:

#!/usr/bin/env python3

from PIL import Image
img = Image.open("5bede6d76859e729190e3f92.jpg")
image_exif = dict(img._getexif())
print('exif: {}'.format(image_exif))
print('orientation1: ' + image_exif.get('274','0'))
print('orientation2: ' + image_exif['274'])

The above returns:

$ ./exif.py 
exif: {256: 2592, 257: 1944, 296: 2, 34665: 226, 271: 'samsung', 272: 'SM-G925F', 305: 'G925FXXU6ERF5', 274: 8, 306: '2018:11:15 23:36:04', 531: 1, 282: (72, 1), 283: (72, 1), 36864: b'0220', 40960: b'0100', 40961: 1, 36867: '2018:11:15 23:36:04', 36868: '2018:11:15 23:36:04', 37381: (1900, 1000), 40962: 2592, 37383: 2, 40963: 1944, 37385: 0, 37386: (220, 100), 41986: 0, 41987: 0, 41989: 22, 41990: 0, 33434: (1, 20), 33437: (19, 10), 42016: 'B05LLHA01PM B05LLHA01PM\n', 34850: 2, 34855: 200, 37500: b'\x07\x00\x01\x00\x07\x00\x04\x00\x00\x000100\x02\x00\x04\x00\x01\x00\x00\x00\x00 \x01\x00\x0c\x00\x04\x00\x01\x00\x00\x00\x00\x00\x00\x00\x10\x00\x05\x00\x01\x00\x00\x00Z\x00\x00\x00@\x00\x04\x00\x01\x00\x00\x00\x00\x00\x00\x00P\x00\x04\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x01\x03\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'}
orientation1: 0
Traceback (most recent call last):
  File "./exif.py", line 8, in <module>
    print('orientation2: ' + image_exif['274'])
KeyError: '274'

Any input would be greatly appreciated! :)

Upvotes: 1

Views: 2047

Answers (1)

DeepSpace
DeepSpace

Reputation: 81654

The keys are integers, '274' is a string. Try image_exif[274]. This, however, will lead to another error since strings and integers can't be concatenated.

Use .format:

print('orientation2: {}'.format(image_exif[274]))

Upvotes: 1

Related Questions