Rotating an image with orientation specified in EXIF using Python without PIL including the thumbnail

后端 未结 6 1271
太阳男子
太阳男子 2020-12-02 09:10

I have the following scenario:

  • I am sending an image from iPhone along with the EXIF information to my Pyhon socket server.
  • I need the image to be pro
6条回答
  •  情歌与酒
    2020-12-02 10:06

    Pretty much the same answer than @scabbiaza, but using transpose instead of rotate (for performance purposes).

    from PIL import Image, ExifTags
    
    try:
        image=Image.open(filepath)
        for orientation in ExifTags.TAGS.keys():
            if ExifTags.TAGS[orientation]=='Orientation':
                break
        exif=dict(image._getexif().items())
    
        if exif[orientation] == 3:
            image=image.transpose(Image.ROTATE_180)
        elif exif[orientation] == 6:
            image=image.transpose(Image.ROTATE_270)
        elif exif[orientation] == 8:
            image=image.transpose(Image.ROTATE_90)
        image.save(filepath)
        image.close()
    
    except (AttributeError, KeyError, IndexError):
        # cases: image don't have getexif
        pass
    

提交回复
热议问题