Skip to content

Instantly share code, notes, and snippets.

@benob
Created May 27, 2019 07:32
Show Gist options
  • Save benob/2cc6ea1394dee1b49aa465fe9e1d9400 to your computer and use it in GitHub Desktop.
Save benob/2cc6ea1394dee1b49aa465fe9e1d9400 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment