diff --git a/pyrit/converter/add_image_text_converter.py b/pyrit/converter/add_image_text_converter.py index 0906c91daa..913ad63b43 100644 --- a/pyrit/converter/add_image_text_converter.py +++ b/pyrit/converter/add_image_text_converter.py @@ -3,6 +3,7 @@ import base64 import logging +import math from io import BytesIO from typing import cast @@ -59,13 +60,14 @@ def __init__( bounding_box (tuple[int, int, int, int] | None): Optional (x1, y1, x2, y2) region to constrain text within. When not set, the full image is used with a default margin. Defaults to None. - rotation (float): Rotation angle in degrees for the text. Defaults to 0.0. + rotation (float): Rotation angle in degrees for the text. Must be finite. Defaults to 0.0. center_text (bool): Whether to center text horizontally and vertically within the bounding box. Defaults to False. Raises: ValueError: If img_to_add is empty, font_name doesn't end with ".ttf", - font_size is invalid, or bounding_box coordinates are invalid. + font_size is invalid, bounding_box coordinates are invalid, + or rotation is non-finite. """ if not img_to_add: raise ValueError("Please provide valid image path") @@ -76,6 +78,8 @@ def __init__( x1, y1, x2, y2 = bounding_box if x2 <= x1 or y2 <= y1: raise ValueError("bounding_box must have x2 > x1 and y2 > y1") + if not math.isfinite(rotation): + raise ValueError(f"rotation must be finite, got {rotation}") self._img_to_add = img_to_add self._font_name = font_name self._font_size = self._font_size_max diff --git a/tests/unit/converter/test_add_image_text_converter.py b/tests/unit/converter/test_add_image_text_converter.py index 504a980f35..e9f7baf5bf 100644 --- a/tests/unit/converter/test_add_image_text_converter.py +++ b/tests/unit/converter/test_add_image_text_converter.py @@ -256,3 +256,12 @@ def test_add_image_text_converter_auto_font_size_no_bounding_box(large_sample_im ) updated_image = converter._add_text_to_image("Auto-sized text on full image") assert updated_image is not None + + +@pytest.mark.parametrize("rotation", [float("nan"), float("inf"), float("-inf")]) +def test_add_image_text_converter_rejects_non_finite_rotation( + image_text_converter_sample_image: str, rotation: float +) -> None: + """Non-finite rotation angles should fail during converter construction.""" + with pytest.raises(ValueError, match="rotation must be finite"): + AddImageTextConverter(img_to_add=image_text_converter_sample_image, rotation=rotation)