Last active
December 17, 2015 06:48
-
-
Save vojd/5567884 to your computer and use it in GitHub Desktop.
Rotate image based on EXIF tags. Useful for iphone uploads
This file contains hidden or 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 | |
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