From 689b8f571899c937eb29cc91921f5da3e690aff7 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 6 Jun 2021 20:29:29 -0700 Subject: [PATCH] Let rotate_by_exif return new exif with orientation=1. --- voussoirkit/imagetools.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/voussoirkit/imagetools.py b/voussoirkit/imagetools.py index d10b014..5d4d360 100644 --- a/voussoirkit/imagetools.py +++ b/voussoirkit/imagetools.py @@ -1,3 +1,4 @@ +import copy import PIL.ExifTags ORIENTATION_KEY = None @@ -35,6 +36,9 @@ def rotate_by_exif(image): ''' Rotate the image according to its exif data, so that it will display correctly even if saved without the exif. + + Returns (image, exif) where exif has the orientation key set to 1, + the upright position, if the rotation was successful. ''' # Thank you Scabbiaza # https://stackoverflow.com/a/26928142 @@ -42,21 +46,38 @@ def rotate_by_exif(image): try: exif = image.getexif() except AttributeError: - return image + return (image, exif) if exif is None: - return image + return (image, exif) try: rotation = exif[ORIENTATION_KEY] except KeyError: - return image + return (image, exif) - if rotation == 3: - image = image.rotate(180, expand=True) + exif = copy.deepcopy(exif) + + if rotation == 1: + pass + elif rotation == 2: + image = image.transpose(PIL.Image.FLIP_LEFT_RIGHT) + elif rotation == 3: + image = image.transpose(PIL.Image.ROTATE_180) + elif rotation == 4: + image = image.transpose(PIL.Image.FLIP_LEFT_RIGHT) + image = image.transpose(PIL.Image.ROTATE_180) + elif rotation == 5: + image = image.transpose(PIL.Image.FLIP_LEFT_RIGHT) + image = image.transpose(PIL.Image.ROTATE_90) elif rotation == 6: - image = image.rotate(270, expand=True) + image = image.transpose(PIL.Image.ROTATE_270) + elif rotation == 7: + image = image.transpose(PIL.Image.FLIP_LEFT_RIGHT) + image = image.transpose(PIL.Image.ROTATE_270) elif rotation == 8: - image = image.rotate(90, expand=True) + image = image.transpose(PIL.Image.ROTATE_90) - return image + exif[ORIENTATION_KEY] = 1 + + return (image, exif)