Skip to content

Commit

Permalink
Add orientation metadata tests
Browse files Browse the repository at this point in the history
To prove that orientation metadata is removed, as that is part of the
expected behaviour now.
  • Loading branch information
Stormheg committed Nov 19, 2023
1 parent d34b04e commit 7ff4433
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tests/test_pillow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from unittest import mock

import filetype
from PIL import ExifTags, ImageChops
from PIL import Image as PILImage
from PIL import ImageChops

from willow.image import (
AvifImageFile,
Expand Down Expand Up @@ -445,6 +445,12 @@ def assert_orientation_landscape_image_is_correct(self, image):
self.assertAlmostEqual(colour[1], 93, delta=25)
self.assertAlmostEqual(colour[2], 65, delta=25)

def assert_orientation_metadata_to_be(self, image, expect_orientation):
image_exif = image.image.getexif()
orientation = image_exif.get(ExifTags.Base.Orientation)

self.assertEqual(orientation, expect_orientation)

def test_jpeg_with_orientation_1(self):
with open("tests/images/orientation/landscape_1.jpg", "rb") as f:
image = PillowImage.open(JPEGImageFile(f))
Expand All @@ -453,13 +459,19 @@ def test_jpeg_with_orientation_1(self):

self.assert_orientation_landscape_image_is_correct(image)

# This is a special case. The image is already in the correct orientation
# so the auto_orient method should not have changed anything.
# The default orientation is 1, so we expect that to be the orientation metadata
self.assert_orientation_metadata_to_be(image, expect_orientation=1)

def test_jpeg_with_orientation_2(self):
with open("tests/images/orientation/landscape_2.jpg", "rb") as f:
image = PillowImage.open(JPEGImageFile(f))

image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_3(self):
with open("tests/images/orientation/landscape_3.jpg", "rb") as f:
Expand All @@ -468,6 +480,7 @@ def test_jpeg_with_orientation_3(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_4(self):
with open("tests/images/orientation/landscape_4.jpg", "rb") as f:
Expand All @@ -476,6 +489,7 @@ def test_jpeg_with_orientation_4(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_5(self):
with open("tests/images/orientation/landscape_5.jpg", "rb") as f:
Expand All @@ -484,6 +498,7 @@ def test_jpeg_with_orientation_5(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_6(self):
with open("tests/images/orientation/landscape_6.jpg", "rb") as f:
Expand All @@ -492,6 +507,7 @@ def test_jpeg_with_orientation_6(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_7(self):
with open("tests/images/orientation/landscape_7.jpg", "rb") as f:
Expand All @@ -500,6 +516,7 @@ def test_jpeg_with_orientation_7(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_8(self):
with open("tests/images/orientation/landscape_8.jpg", "rb") as f:
Expand All @@ -508,3 +525,4 @@ def test_jpeg_with_orientation_8(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)
19 changes: 19 additions & 0 deletions tests/test_wand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest import mock

import filetype
from PIL import ExifTags
from PIL import Image as PILImage
from wand import version as WAND_VERSION

Expand Down Expand Up @@ -448,6 +449,12 @@ def assert_orientation_landscape_image_is_correct(self, image):
self.assertAlmostEqual(colour.green * 255, 93, delta=15)
self.assertAlmostEqual(colour.blue * 255, 65, delta=15)

def assert_orientation_metadata_to_be(self, image, expect_orientation=None):
image_exif = image.image.getexif()
orientation = image_exif.get(ExifTags.Base.Orientation)

self.assertEqual(orientation, expect_orientation)

def test_jpeg_with_orientation_1(self):
with open("tests/images/orientation/landscape_1.jpg", "rb") as f:
image = WandImage.open(JPEGImageFile(f))
Expand All @@ -456,13 +463,19 @@ def test_jpeg_with_orientation_1(self):

self.assert_orientation_landscape_image_is_correct(image)

# This is a special case. The image is already in the correct orientation
# so the auto_orient method should not have changed anything.
# The default orientation is 1, so we expect that to be the orientation metadata
self.assert_orientation_metadata_to_be(image, expect_orientation=1)

def test_jpeg_with_orientation_2(self):
with open("tests/images/orientation/landscape_2.jpg", "rb") as f:
image = WandImage.open(JPEGImageFile(f))

image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_3(self):
with open("tests/images/orientation/landscape_3.jpg", "rb") as f:
Expand All @@ -471,6 +484,7 @@ def test_jpeg_with_orientation_3(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_4(self):
with open("tests/images/orientation/landscape_4.jpg", "rb") as f:
Expand All @@ -479,6 +493,7 @@ def test_jpeg_with_orientation_4(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_5(self):
with open("tests/images/orientation/landscape_5.jpg", "rb") as f:
Expand All @@ -487,6 +502,7 @@ def test_jpeg_with_orientation_5(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_6(self):
with open("tests/images/orientation/landscape_6.jpg", "rb") as f:
Expand All @@ -495,6 +511,7 @@ def test_jpeg_with_orientation_6(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_7(self):
with open("tests/images/orientation/landscape_7.jpg", "rb") as f:
Expand All @@ -503,6 +520,7 @@ def test_jpeg_with_orientation_7(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

def test_jpeg_with_orientation_8(self):
with open("tests/images/orientation/landscape_8.jpg", "rb") as f:
Expand All @@ -511,3 +529,4 @@ def test_jpeg_with_orientation_8(self):
image = image.auto_orient()

self.assert_orientation_landscape_image_is_correct(image)
self.assert_orientation_metadata_to_be(image, None)

0 comments on commit 7ff4433

Please sign in to comment.