diff --git a/Tests/images/13246.png b/Tests/images/13246.png new file mode 100644 index 00000000000..364c14aace1 Binary files /dev/null and b/Tests/images/13246.png differ diff --git a/Tests/images/expected.jpg b/Tests/images/expected.jpg new file mode 100644 index 00000000000..8065e9226c5 Binary files /dev/null and b/Tests/images/expected.jpg differ diff --git a/Tests/test_image.py b/Tests/test_image.py index f0861bb4f81..150d78e5551 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1024,6 +1024,75 @@ def test_close_graceful(self, caplog): assert len(caplog.records) == 0 assert im.fp is None + def test_7604(self, tmp_path): + temp_file = str(tmp_path / "temp.jpg") + + from typing import Tuple + EXIF_ORIENTATION_BYTE_OFFSET = 0x0112 + + def make_white_copy(image: Image, background_color: Tuple[int, int, int]=(255, 255, 255)) -> Image: + """Ensure we get correct background when cropping image.""" + width = image.width + height = image.height + is_landscape = width > height + is_portrait = height > width + x_pos = 0 + y_pos = 0 + if width != height: + if is_landscape: + desired_width = width + desired_height = int(height * (width / height)) + x_pos = 0 + y_pos = int((desired_width - height) / 2) + elif is_portrait: + desired_width = int(width * (height / width)) + desired_height = height + x_pos = int((desired_height - width) / 2) + y_pos = 0 + + new_img = Image.new("RGB", (desired_width, desired_height), background_color) + new_img.paste(image, (x_pos, y_pos)) + return new_img + + + def transpose_by_exif(image: Image) -> Image: + """Use exif data if present to perform action on image.""" + orientation = image.getexif().get(EXIF_ORIENTATION_BYTE_OFFSET) + if orientation is not None and orientation > 1: + transposition_action = { + 2: Image.FLIP_LEFT_RIGHT, + 3: Image.ROTATE_180, + 4: Image.FLIP_TOP_BOTTOM, + 5: Image.TRANSPOSE, + 6: Image.ROTATE_270, + 7: Image.TRANSVERSE, + 8: Image.ROTATE_90, + }.get(orientation) + image = image.transpose(transposition_action) + return image + + + def resize(image: Image) -> Image: + """Resize Image to dimensions.""" + size = (128, 128) + image.thumbnail(size) + return image + + saved_image = None + img = Image.open("Tests/images/13246.png") + managed_image = resize(make_white_copy(img)) + managed_image.save( + temp_file, + optimize=True, + quality=100, + jfif_unit=1, + dpi=(96, 96), + jfif_density=(96, 96), + ) + with Image.open(temp_file) as reloaded: + assert_image_similar_tofile(reloaded, "Tests/images/expected.jpg", 0.01) + + class MockEncoder: pass