Delete EXIF orientation tag after transposing

This commit is contained in:
Andrew Murray 2019-04-01 18:49:09 +11:00
parent c8257b30dd
commit 38fb9b1030
2 changed files with 19 additions and 5 deletions

View File

@ -1,7 +1,8 @@
from .helper import PillowTestCase, hopper from .helper import PillowTestCase, hopper
from PIL import ImageOps
from PIL import Image from PIL import Image
from PIL import ImageFile
from PIL import ImageOps
try: try:
from PIL import _webp from PIL import _webp
@ -240,8 +241,20 @@ class TestImageOps(PillowTestCase):
im = Image.open("Tests/images/hopper_orientation_"+str(i)+ext) im = Image.open("Tests/images/hopper_orientation_"+str(i)+ext)
orientations.append(im) orientations.append(im)
for im in orientations: 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) transposed_im = ImageOps.exif_transpose(im)
self.assert_image_similar(base_im, transposed_im, 17) 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 # Repeat the operation, to test that it does not keep transposing
transposed_im2 = ImageOps.exif_transpose(transposed_im) transposed_im2 = ImageOps.exif_transpose(transposed_im)

View File

@ -17,7 +17,7 @@
# See the README file for information on usage and redistribution. # See the README file for information on usage and redistribution.
# #
from . import Image from . import Image, ImageFile
from ._util import isStringType from ._util import isStringType
import operator import operator
import functools import functools
@ -532,8 +532,8 @@ def exif_transpose(image):
:param image: The image to transpose. :param image: The image to transpose.
:return: An image. :return: An image.
""" """
if not hasattr(image, '_exif_transposed') and hasattr(image, '_getexif'): if isinstance(image, ImageFile.ImageFile):
exif = image._getexif() exif = image.getexif()
if exif: if exif:
orientation = exif.get(0x0112) orientation = exif.get(0x0112)
method = { method = {
@ -547,6 +547,7 @@ def exif_transpose(image):
}.get(orientation) }.get(orientation)
if method is not None: if method is not None:
transposed_image = image.transpose(method) transposed_image = image.transpose(method)
transposed_image._exif_transposed = True del exif[0x0112]
transposed_image.info["exif"] = exif.tobytes()
return transposed_image return transposed_image
return image.copy() return image.copy()