Skip to content

Instantly share code, notes, and snippets.

@vojd
Last active December 17, 2015 06:48
Show Gist options
  • Save vojd/5567884 to your computer and use it in GitHub Desktop.
Save vojd/5567884 to your computer and use it in GitHub Desktop.
Rotate image based on EXIF tags. Useful for iphone uploads
from PIL import Image
def rotate_image(image):
"""
Rotates an image based on EXIF information. Used to mitigate non-rotated landscape photos taken from iPhone.
:Args ``image`` Image, opened using PIL:
"""
ANGLES = {
6: -90,
5: 90,
}
try:
orientation = image._getexif().get(274, 0)
except Exception as e:
print 'orientation',e
orientation = 0
angle = ANGLES.get(orientation, 0)
print 'angle' , angle
if angle != 0:
try:
rot_img = image.rotate(angle, expand=0)
rot_img.save(image.filename)
return True
except Exception as e:
print e
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment