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
6 changes: 4 additions & 2 deletions pyrit/converter/add_image_text_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/converter/test_add_image_text_font_size_validation.py
Original file line number Diff line number Diff line change
@@ -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)