Skip to content

Commit eebae30

Browse files
authored
Merge pull request #175 from wkentaro/fix/text-in-rectangle-lb-align
fix: align lb/lb- text to rectangle left edge in text_in_rectangle
2 parents 60beae8 + 8ae5edd commit eebae30

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

imgviz/draw/_text_in_rectangle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ def text_in_rectangle_aabb(
6363
elif loc == "rt+":
6464
yx = (y1 - tsize[0] - 2, x2 - tsize[1] - 2)
6565
elif loc == "lb":
66-
yx = (y2 - tsize[0] - 2, 0)
66+
yx = (y2 - tsize[0] - 2, x1)
6767
elif loc == "lb-":
68-
yx = (y2, 0)
68+
yx = (y2, x1)
6969
elif loc == "rb":
7070
yx = (y2 - tsize[0] - 2, x2 - tsize[1] - 2)
7171
elif loc == "rb-":

tests/unit/draw/_text_in_rectangle_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
from typing import Literal
2+
13
import numpy as np
4+
import pytest
25

36
import imgviz
47

@@ -15,3 +18,31 @@ def test_text_in_rectangle() -> None:
1518
assert res.shape == img.shape
1619
assert res.dtype == np.uint8
1720
assert not np.array_equal(res, img)
21+
22+
23+
@pytest.mark.parametrize("loc", ["lt", "lt+", "lb", "lb-"])
24+
def test_text_in_rectangle_aabb_left_aligns_to_x1(
25+
loc: Literal["lt", "lt+", "lb", "lb-"],
26+
) -> None:
27+
yx1 = (20, 50)
28+
yx2 = (80, 150)
29+
30+
aabb = imgviz.draw.text_in_rectangle_aabb(
31+
yx1=yx1, yx2=yx2, loc=loc, text="Hi", size=10
32+
)
33+
34+
assert aabb.x1 == yx1[1]
35+
36+
37+
@pytest.mark.parametrize("loc", ["rt", "rt+", "rb", "rb-"])
38+
def test_text_in_rectangle_aabb_right_aligns_to_x2(
39+
loc: Literal["rt", "rt+", "rb", "rb-"],
40+
) -> None:
41+
yx1 = (20, 50)
42+
yx2 = (80, 150)
43+
44+
aabb = imgviz.draw.text_in_rectangle_aabb(
45+
yx1=yx1, yx2=yx2, loc=loc, text="Hi", size=10
46+
)
47+
48+
assert aabb.x2 == yx2[1] - 1 # text right edge sits one pixel inside x2

0 commit comments

Comments
 (0)