Skip to content
Open
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
14 changes: 11 additions & 3 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 All @@ -74,7 +75,14 @@ def _apply_transform(self, image: Image.Image) -> Image.Image:

Returns:
PIL.Image.Image: The resized image.

Raises:
ValueError: If the resizing would result in invalid dimensions (less than 1 pixel).
"""
new_width = int(image.width * self._scale_factor)
new_height = int(image.height * self._scale_factor)
if new_width < 1 or new_height < 1:
raise ValueError(
f"Resizing would result in invalid dimensions (less than 1 pixel): {new_width}x{new_height}"
)
return image.resize((new_width, new_height), Image.Resampling.LANCZOS)
13 changes: 11 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,12 @@ 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)


def test_image_resizing_converter_invalid_dimensions():
"""Test that scale factor yielding zero dimensions raises ValueError."""
converter = ImageResizingConverter(scale_factor=0.5)
# A 1x1 image scaled by 0.5 becomes 0x0
image = Image.new("RGB", (1, 1))
with pytest.raises(ValueError, match="Resizing would result in invalid dimensions"):
converter._apply_transform(image)