mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-18 12:03:12 +03:00
Allow exif_transpose to work on Image instances as well as ImageFile
This commit is contained in:
parent
38fb9b1030
commit
8f0db65cd7
|
@ -240,22 +240,26 @@ class TestImageOps(PillowTestCase):
|
||||||
for i in range(2, 9):
|
for i in range(2, 9):
|
||||||
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 i, orientation_im in enumerate(orientations):
|
||||||
if im is base_im:
|
for im in [
|
||||||
self.assertNotIn("exif", im.info)
|
orientation_im, # ImageFile
|
||||||
else:
|
orientation_im.copy() # Image
|
||||||
original_exif = im.info["exif"]
|
]:
|
||||||
transposed_im = ImageOps.exif_transpose(im)
|
if i == 0:
|
||||||
self.assert_image_similar(base_im, transposed_im, 17)
|
self.assertNotIn("exif", im.info)
|
||||||
if im is base_im:
|
else:
|
||||||
self.assertNotIn("exif", im.info)
|
original_exif = im.info["exif"]
|
||||||
else:
|
transposed_im = ImageOps.exif_transpose(im)
|
||||||
self.assertNotEqual(transposed_im.info["exif"], original_exif)
|
self.assert_image_similar(base_im, transposed_im, 17)
|
||||||
|
if i == 0:
|
||||||
|
self.assertNotIn("exif", im.info)
|
||||||
|
else:
|
||||||
|
self.assertNotEqual(transposed_im.info["exif"], original_exif)
|
||||||
|
|
||||||
exif = ImageFile.Exif()
|
exif = ImageFile.Exif()
|
||||||
exif.load(transposed_im.info["exif"])
|
exif.load(transposed_im.info["exif"])
|
||||||
self.assertNotIn(0x0112, 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)
|
||||||
self.assert_image_equal(transposed_im2, transposed_im)
|
self.assert_image_equal(transposed_im2, transposed_im)
|
||||||
|
|
|
@ -532,22 +532,22 @@ def exif_transpose(image):
|
||||||
:param image: The image to transpose.
|
:param image: The image to transpose.
|
||||||
:return: An image.
|
:return: An image.
|
||||||
"""
|
"""
|
||||||
if isinstance(image, ImageFile.ImageFile):
|
if "exif" in image.info:
|
||||||
exif = image.getexif()
|
exif = ImageFile.Exif()
|
||||||
if exif:
|
exif.load(image.info["exif"])
|
||||||
orientation = exif.get(0x0112)
|
orientation = exif.get(0x0112)
|
||||||
method = {
|
method = {
|
||||||
2: Image.FLIP_LEFT_RIGHT,
|
2: Image.FLIP_LEFT_RIGHT,
|
||||||
3: Image.ROTATE_180,
|
3: Image.ROTATE_180,
|
||||||
4: Image.FLIP_TOP_BOTTOM,
|
4: Image.FLIP_TOP_BOTTOM,
|
||||||
5: Image.TRANSPOSE,
|
5: Image.TRANSPOSE,
|
||||||
6: Image.ROTATE_270,
|
6: Image.ROTATE_270,
|
||||||
7: Image.TRANSVERSE,
|
7: Image.TRANSVERSE,
|
||||||
8: Image.ROTATE_90
|
8: Image.ROTATE_90
|
||||||
}.get(orientation)
|
}.get(orientation)
|
||||||
if method is not None:
|
if method is not None:
|
||||||
transposed_image = image.transpose(method)
|
transposed_image = image.transpose(method)
|
||||||
del exif[0x0112]
|
del exif[0x0112]
|
||||||
transposed_image.info["exif"] = exif.tobytes()
|
transposed_image.info["exif"] = exif.tobytes()
|
||||||
return transposed_image
|
return transposed_image
|
||||||
return image.copy()
|
return image.copy()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user