Skip to content

Instantly share code, notes, and snippets.

@benob
Created May 27, 2019 07:32
Show Gist options
  • Save benob/0ef5d17f664d97cd70b7206c43ce184a to your computer and use it in GitHub Desktop.
Save benob/0ef5d17f664d97cd70b7206c43ce184a to your computer and use it in GitHub Desktop.
Rotate pictures according to EXIF orientation tag, in python using PIL. Supports all 8 orientations with a simple transposition operation.
# Proper exif rotation with PIL. Handles all the cases in https://github.com/recurser/exif-orientation-examples
from PIL import Image
image = Image.open(filename)
exif = image._getexif()
ORIENTATION = 274
if exif is not None and ORIENTATION in exif:
orientation = exif[ORIENTATION]
method = {2: Image.FLIP_LEFT_RIGHT, 4: Image.FLIP_TOP_BOTTOM, 8: Image.ROTATE_90, 3: Image.ROTATE_180, 6: Image.ROTATE_270, 5: Image.TRANSPOSE, 7: Image.TRANSVERSE}
if orientation in method:
image = image.transpose(method[orientation])
image.show()
@pabluch
Copy link

pabluch commented Jul 31, 2019

Thanks mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment