Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pyrit/converter/image_resizing_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the MIT license.

import logging
import math
from typing import Literal

from PIL import Image
Expand Down Expand Up @@ -44,10 +45,10 @@ def __init__(
Defaults to 0.5 (halve the image dimensions).

Raises:
ValueError: If unsupported output format is specified, or if scale factor is not positive.
ValueError: If unsupported output format is specified, or if scale factor is not a positive finite number.
"""
if scale_factor <= 0:
raise ValueError(f"Scale factor must be positive, got {scale_factor}")
if not math.isfinite(scale_factor) or scale_factor <= 0:
raise ValueError(f"Scale factor must be a positive finite number, got {scale_factor}")
self._scale_factor = scale_factor
super().__init__(output_format=output_format)

Expand Down Expand Up @@ -75,6 +76,6 @@ def _apply_transform(self, image: Image.Image) -> Image.Image:
Returns:
PIL.Image.Image: The resized image.
"""
new_width = int(image.width * self._scale_factor)
new_height = int(image.height * self._scale_factor)
new_width = max(1, int(image.width * self._scale_factor))
new_height = max(1, int(image.height * self._scale_factor))
return image.resize((new_width, new_height), Image.Resampling.LANCZOS)
24 changes: 22 additions & 2 deletions tests/unit/converter/test_image_resizing_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def test_image_resizing_converter_initialization_output_format_validation():

def test_image_resizing_converter_initialization_scale_factor_validation():
"""Test validation of scale_factor parameter."""
for invalid_scale_factor in [0.0, -0.1, -1.0, -100.0]:
with pytest.raises(ValueError, match="Scale factor must be positive"):
for invalid_scale_factor in [0.0, -0.1, -1.0, -100.0, float("nan"), float("inf"), float("-inf")]:
with pytest.raises(ValueError, match="Scale factor must be a positive finite number"):
ImageResizingConverter(scale_factor=invalid_scale_factor)

for valid_scale_factor in [0.1, 0.5, 1.0, 2.0, 10.0]:
Expand Down Expand Up @@ -229,3 +229,23 @@ async def test_image_resizing_converter_output_dimensions(sample_image_bytes):
expected_width = int(original_size[0] * scale_factor)
expected_height = int(original_size[1] * scale_factor)
assert resized_image.size == (expected_width, expected_height)


@pytest.mark.parametrize(
("input_size", "expected_size"),
[
((1, 1), (1, 1)),
((1, 8), (1, 4)),
((8, 1), (4, 1)),
],
)
def test_image_resizing_converter_minimum_output_dimensions(
input_size: tuple[int, int], expected_size: tuple[int, int]
) -> None:
"""Test that each output dimension is clamped to at least one pixel."""
converter = ImageResizingConverter(scale_factor=0.5)
image = Image.new("RGB", input_size)

resized_image = converter._apply_transform(image)

assert resized_image.size == expected_size