Created
October 10, 2017 13:50
-
-
Save wiedi/b10ef0a085b05c49421117c99c8df69c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ExifTags | |
def exif_rotate(img): | |
exif_tags = dict([(x[1], x[0]) for x in ExifTags.TAGS.items()]) | |
orientation = 1 | |
try: | |
orientation = img._getexif()[exif_tags['Orientation']] | |
except: | |
pass | |
if orientation == 1: | |
return img | |
elif orientation == 2: | |
return img.transpose(Image.FLIP_LEFT_RIGHT) | |
elif orientation == 3: | |
return img.transpose(Image.ROTATE_180) | |
elif orientation == 4: | |
return img.transpose(Image.FLIP_TOP_BOTTOM) | |
elif orientation == 5: | |
return img.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.ROTATE_90) | |
elif orientation == 6: | |
return img.transpose(Image.ROTATE_270) | |
elif orientation == 7: | |
return img.transpose(Image.FLIP_TOP_BOTTOM).transpose(Image.ROTATE_90) | |
elif orientation == 8: | |
return img.transpose(Image.ROTATE_90) | |
return img | |
img = Image.open("bla.jpg") | |
img = exif_rotate(img) | |
img.thumbnail((720, 720)) | |
img.save("foo.jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment