Skip to content

Commit 0b122fa

Browse files
ThryLoxCopilot
andauthored
FIX Clamp ImageResizingConverter output dimensions (#2169)
Co-authored-by: thrylox <ThryLox@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 90fa2f8 commit 0b122fa

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

pyrit/converter/image_resizing_converter.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Licensed under the MIT license.
33

44
import logging
5+
import math
56
from typing import Literal
67

78
from PIL import Image
@@ -44,10 +45,10 @@ def __init__(
4445
Defaults to 0.5 (halve the image dimensions).
4546
4647
Raises:
47-
ValueError: If unsupported output format is specified, or if scale factor is not positive.
48+
ValueError: If unsupported output format is specified, or if scale factor is not a positive finite number.
4849
"""
49-
if scale_factor <= 0:
50-
raise ValueError(f"Scale factor must be positive, got {scale_factor}")
50+
if not math.isfinite(scale_factor) or scale_factor <= 0:
51+
raise ValueError(f"Scale factor must be a positive finite number, got {scale_factor}")
5152
self._scale_factor = scale_factor
5253
super().__init__(output_format=output_format)
5354

@@ -75,6 +76,6 @@ def _apply_transform(self, image: Image.Image) -> Image.Image:
7576
Returns:
7677
PIL.Image.Image: The resized image.
7778
"""
78-
new_width = int(image.width * self._scale_factor)
79-
new_height = int(image.height * self._scale_factor)
79+
new_width = max(1, int(image.width * self._scale_factor))
80+
new_height = max(1, int(image.height * self._scale_factor))
8081
return image.resize((new_width, new_height), Image.Resampling.LANCZOS)

tests/unit/converter/test_image_resizing_converter.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def test_image_resizing_converter_initialization_output_format_validation():
5252

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

5959
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):
229229
expected_width = int(original_size[0] * scale_factor)
230230
expected_height = int(original_size[1] * scale_factor)
231231
assert resized_image.size == (expected_width, expected_height)
232+
233+
234+
@pytest.mark.parametrize(
235+
("input_size", "expected_size"),
236+
[
237+
((1, 1), (1, 1)),
238+
((1, 8), (1, 4)),
239+
((8, 1), (4, 1)),
240+
],
241+
)
242+
def test_image_resizing_converter_minimum_output_dimensions(
243+
input_size: tuple[int, int], expected_size: tuple[int, int]
244+
) -> None:
245+
"""Test that each output dimension is clamped to at least one pixel."""
246+
converter = ImageResizingConverter(scale_factor=0.5)
247+
image = Image.new("RGB", input_size)
248+
249+
resized_image = converter._apply_transform(image)
250+
251+
assert resized_image.size == expected_size

0 commit comments

Comments
 (0)