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

后端 未结 6 1203
太阳男子
太阳男子 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 09:49

    This solution works for me: PIL thumbnail is rotating my image?

    Don't need to check if it's iPhone or iPad: if photo has orientation tag – rotate it.

    from PIL import Image, ExifTags
    
    try:
        image=Image.open(filepath)
    
        for orientation in ExifTags.TAGS.keys():
            if ExifTags.TAGS[orientation]=='Orientation':
                break
        
        exif = image._getexif()
    
        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(filepath)
        image.close()
    except (AttributeError, KeyError, IndexError):
        # cases: image don't have getexif
        pass
    

    Before:

    Before

    After: After

提交回复
热议问题