diff --git a/pyrit/converter/add_image_text_converter.py b/pyrit/converter/add_image_text_converter.py index 87715bd9d0..0906c91daa 100644 --- a/pyrit/converter/add_image_text_converter.py +++ b/pyrit/converter/add_image_text_converter.py @@ -65,7 +65,7 @@ def __init__( Raises: ValueError: If img_to_add is empty, font_name doesn't end with ".ttf", - font_size tuple is invalid, or bounding_box coordinates are invalid. + font_size is invalid, or bounding_box coordinates are invalid. """ if not img_to_add: raise ValueError("Please provide valid image path") @@ -119,7 +119,7 @@ def _extract_font_size(self, font_size: int | tuple[int, int]) -> None: font_size (int | tuple[int, int]): Fixed size or (min, max) range. Raises: - ValueError: If font_size tuple is invalid. + ValueError: If font_size is not positive or the tuple range is invalid. """ if isinstance(font_size, tuple): if len(font_size) != 2 or font_size[0] > font_size[1] or font_size[0] < 1: @@ -128,6 +128,8 @@ def _extract_font_size(self, font_size: int | tuple[int, int]) -> None: self._font_size_max = font_size[1] self._auto_font_size = True else: + if font_size < 1: + raise ValueError("font_size must be greater than 0") self._font_size_min = font_size self._font_size_max = font_size self._auto_font_size = False diff --git a/tests/unit/converter/test_add_image_text_font_size_validation.py b/tests/unit/converter/test_add_image_text_font_size_validation.py new file mode 100644 index 0000000000..c69ddc28c0 --- /dev/null +++ b/tests/unit/converter/test_add_image_text_font_size_validation.py @@ -0,0 +1,16 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT license. + +import pytest +from PIL import Image + +from pyrit.converter import AddImageTextConverter + + +@pytest.mark.parametrize("font_size", [0, -1]) +def test_add_image_text_converter_rejects_non_positive_fixed_font_size(tmp_path, font_size): + image_path = tmp_path / "test.png" + Image.new("RGB", (32, 32)).save(image_path) + + with pytest.raises(ValueError, match="font_size must be greater than 0"): + AddImageTextConverter(img_to_add=str(image_path), font_size=font_size)