Python3 Exif方向键错误:"274" [英] Python3 Exif orientation KeyError: '274'

查看:393
本文介绍了Python3 Exif方向键错误:"274"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从图像中拉出Exif方向,但是由于某种原因,我一直收到KeyError:"274",即使它似乎在字典中的值为"8".

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'.

摘要:

#!/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'])

以上返回:

$ ./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! :)

推荐答案

键是整数,'274'是字符串.尝试image_exif[274].但是,这将导致另一个错误,因为字符串和整数不能串联.

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.

使用.format:

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

这篇关于Python3 Exif方向键错误:"274"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆