Created
May 27, 2019 07:32
-
-
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.
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
# 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
Thanks mate!