Created
July 28, 2014 18:47
-
-
Save blorenz/cf54986669acb8dc778d to your computer and use it in GitHub Desktop.
Rotating photos based on EXIF data
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 | |
photo = Image.open(args['filename']) | |
photo_exif = None | |
try: | |
# See if this image has any exif | |
photo_exif = Image.open(args['filename'])._getexif() | |
except: | |
pass | |
if photo_exif: | |
try: | |
exif=dict((ExifTags.TAGS[k], v) for k, v in photo_exif.items() if k in ExifTags.TAGS) | |
if exif['Orientation'] == 6: | |
photo=photo.rotate(-90, expand=True) | |
if exif['Orientation'] == 8: | |
photo=photo.rotate(90, expand=True) | |
if exif['Orientation'] == 3: | |
photo=photo.rotate(180, expand=True) | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment