From 38fb9b103035add4a3a8500cd988ead0b8ad3099 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 1 Apr 2019 18:49:09 +1100 Subject: [PATCH] Delete EXIF orientation tag after transposing --- Tests/test_imageops.py | 15 ++++++++++++++- src/PIL/ImageOps.py | 9 +++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index c0ac12f6c..f6fddde60 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -1,7 +1,8 @@ from .helper import PillowTestCase, hopper -from PIL import ImageOps from PIL import Image +from PIL import ImageFile +from PIL import ImageOps try: from PIL import _webp @@ -240,8 +241,20 @@ class TestImageOps(PillowTestCase): im = Image.open("Tests/images/hopper_orientation_"+str(i)+ext) orientations.append(im) for im in orientations: + if im is base_im: + self.assertNotIn("exif", im.info) + else: + original_exif = im.info["exif"] transposed_im = ImageOps.exif_transpose(im) self.assert_image_similar(base_im, transposed_im, 17) + if im is base_im: + self.assertNotIn("exif", im.info) + else: + self.assertNotEqual(transposed_im.info["exif"], original_exif) + + exif = ImageFile.Exif() + exif.load(transposed_im.info["exif"]) + self.assertNotIn(0x0112, exif) # Repeat the operation, to test that it does not keep transposing transposed_im2 = ImageOps.exif_transpose(transposed_im) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index afbe3e2c0..147f2821d 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -17,7 +17,7 @@ # See the README file for information on usage and redistribution. # -from . import Image +from . import Image, ImageFile from ._util import isStringType import operator import functools @@ -532,8 +532,8 @@ def exif_transpose(image): :param image: The image to transpose. :return: An image. """ - if not hasattr(image, '_exif_transposed') and hasattr(image, '_getexif'): - exif = image._getexif() + if isinstance(image, ImageFile.ImageFile): + exif = image.getexif() if exif: orientation = exif.get(0x0112) method = { @@ -547,6 +547,7 @@ def exif_transpose(image): }.get(orientation) if method is not None: transposed_image = image.transpose(method) - transposed_image._exif_transposed = True + del exif[0x0112] + transposed_image.info["exif"] = exif.tobytes() return transposed_image return image.copy()