Skip to content

Instantly share code, notes, and snippets.

@wiedi
Created October 10, 2017 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiedi/b10ef0a085b05c49421117c99c8df69c to your computer and use it in GitHub Desktop.
Save wiedi/b10ef0a085b05c49421117c99c8df69c to your computer and use it in GitHub Desktop.
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