Skip to content
Merged
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
8 changes: 6 additions & 2 deletions pyrit/converter/add_image_text_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import base64
import logging
import math
from io import BytesIO
from typing import cast

Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/converter/test_add_image_text_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading