iPhone照片上传到服务器被逆时针旋转90度的问题

发现用iPhone直接发邮件写博客时,在手机上选的照片,竖版照片到了服务器自动旋转了,就是这篇博客(见识了什么叫干净得变态)

总之就是里面有一个什么exif信息,有一个啥6的值。

stackoverflow上弄了一段代码搞定:

 
try:
    image = Image.open(attachment_path)
    for orientation in ExifTags.TAGS.keys():
    if ExifTags.TAGS[orientation] == 'Orientation':
        break
    exif = dict(image._getexif().items())
    if exif[orientation] == 3:
        image = image.rotate(180, expand=True)
    elif exif[orientation] == 6:
        image = image.rotate(270, expand=True)
    elif exif[orientation] == 8:
        image = image.rotate(90, expand=True)
    image.save(attachment_path)
    image.close()
except (AttributeError, KeyError, IndexError):
    # cases: image don't have getexif
    pass

第6行那个exif[orientation],orientation不知道哪来的,莫非是for内循环的值跑外面了?写了这么多年python,没看明白,IDE也警告,但管用,先懒得追究了。

输入类似这样的地址 "name@something.com"
输入类似这样的地址"http://someaddress.com"
验证码 不区分大小写(看不清)