Skip to content

Instantly share code, notes, and snippets.

@blorenz
Created July 28, 2014 18:47
Show Gist options
  • Save blorenz/cf54986669acb8dc778d to your computer and use it in GitHub Desktop.
Save blorenz/cf54986669acb8dc778d to your computer and use it in GitHub Desktop.
Rotating photos based on EXIF data
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