diff --git a/pyrit/converter/image_resizing_converter.py b/pyrit/converter/image_resizing_converter.py index dbe361f0c9..6c5a17360c 100644 --- a/pyrit/converter/image_resizing_converter.py +++ b/pyrit/converter/image_resizing_converter.py @@ -2,6 +2,7 @@ # Licensed under the MIT license. import logging +import math from typing import Literal from PIL import Image @@ -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) @@ -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) diff --git a/tests/unit/converter/test_image_resizing_converter.py b/tests/unit/converter/test_image_resizing_converter.py index dc88f8aa43..4eb2445b8f 100644 --- a/tests/unit/converter/test_image_resizing_converter.py +++ b/tests/unit/converter/test_image_resizing_converter.py @@ -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]: @@ -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