【发布时间】:2022-03-28 18:58:36
【问题描述】:
上传图片时出现内部服务器错误。然后去heroku日志我看到这个KeyError
这是我的代码:
if request.method == "POST":
user_id = session["user_id"]
if request.files['photo']:
filename = photos.save(request.files['photo'])
extension = os.path.splitext(filename)[1]
foo = Image.open("static/img/"+filename)
exifimage = foo._getexif()
if exifimage:
for orientation in ExifTags.TAGS.keys() :
if ExifTags.TAGS[orientation]=='Orientation':
break
exif=dict(exifimage.items())
if exif[orientation] == 3 :
foo=foo.rotate(180, expand=True)
elif exif[orientation] == 6 :
foo=foo.rotate(270, expand=True)
elif exif[orientation] == 8 :
foo=foo.rotate(90, expand=True)
这是完整的错误:
if exif[orientation] == 3 :
KeyError: 274
为什么会发生这种情况?提前致谢
【问题讨论】:
-
请提供minimal reproducible example。您从该错误消息中了解到什么?
-
根据该错误信息,您认为发生错误时
orientation的值是多少?在exif中用作键是否有意义?为什么或为什么不? -
@AMC 此错误仅发生在从 Internet 下载的图像上,例如,如果您从相机胶卷上传图像,则不会返回任何错误
-
@KarlKnechtel 我对这些东西有点菜鸟,但我知道我需要这段代码,否则图像会被旋转。此错误仅发生在从 Internet 下载的图像中,如果您从相机胶卷上传照片,它将正常工作
-
好的,但我问了 具体 问题,我是出于原因。你对 exif 数据格式了解多少?你究竟在哪里需要帮助?像“为什么”这样的问题在真空中是不可行的;我们需要知道您所处的位置,以便以对您有用的方式解释事情。
标签: python python-3.x image exif