Skip to content

Commit

Permalink
Test 7604
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 5, 2023
1 parent 0a66b98 commit f475a4a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Binary file added Tests/images/13246.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/expected.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f475a4a

Please sign in to comment.