Skip to content

Commit 0f78d19

Browse files
committed
Add bbox benchmark and additional tests
1 parent 8a37d87 commit 0f78d19

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Tests/benchmarks.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,35 @@ def make_pillow_image(
8787
return im.convert(mode)
8888

8989

90+
BBOX_SCENARIOS: dict[str, tuple[float, str] | None] = {
91+
"half-centered": (0.5, "center"),
92+
"small-centered": (0.1, "center"),
93+
"half-corner": (0.5, "corner"),
94+
"empty": None,
95+
}
96+
97+
98+
def make_bbox_image(
99+
mode: str,
100+
size: tuple[int, int],
101+
region: tuple[float, str] | None,
102+
) -> Image.Image:
103+
im = Image.new(mode, size, 0)
104+
if region is None:
105+
return im
106+
fraction, placement = region
107+
w, h = size
108+
bw, bh = max(1, round(w * fraction)), max(1, round(h * fraction))
109+
if placement == "center":
110+
left, top = (w - bw) // 2, (h - bh) // 2
111+
else:
112+
left, top = w - bw, h - bh
113+
nbands = len(im.getbands())
114+
color = (255, 128, 64, 255)[:nbands] if nbands > 1 else 255
115+
im.paste(color, (left, top, left + bw - 1, top + bh - 1))
116+
return im
117+
118+
90119
@pytest.mark.benchmark(group="composition")
91120
@pytest.mark.parametrize("mode", MODES)
92121
@pytest.mark.parametrize("size", SIZES, ids=_format_size)
@@ -431,6 +460,21 @@ def test_offset(bench: BenchmarkFixture, mode: str, size: tuple[int, int]) -> No
431460
bench(ImageChops.offset, im, 123, 45)
432461

433462

463+
@pytest.mark.benchmark(group="bbox")
464+
@pytest.mark.parametrize("scenario", list(BBOX_SCENARIOS))
465+
@pytest.mark.parametrize("mode", ["L", "LA", "I;16", "RGB", "RGBA"])
466+
@pytest.mark.parametrize("size", SIZES, ids=_format_size)
467+
def test_getbbox(
468+
bench: BenchmarkFixture,
469+
mode: str,
470+
size: tuple[int, int],
471+
scenario: str,
472+
) -> None:
473+
im = make_bbox_image(mode, size, BBOX_SCENARIOS[scenario])
474+
bench.extra_info["label"] = [scenario]
475+
bench(im.getbbox)
476+
477+
434478
@pytest.mark.benchmark(group="histogram")
435479
@pytest.mark.parametrize("mode", MODES)
436480
@pytest.mark.parametrize("size", SIZES, ids=_format_size)

Tests/test_image_getbbox.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@ def check(im: Image.Image, fill_color: int | tuple[int, ...]) -> None:
4444
check(im, (255, 255))
4545

4646

47+
@pytest.mark.parametrize("mode", ("L", "RGB", "RGBA", "I", "F"))
48+
@pytest.mark.parametrize(
49+
"box",
50+
(
51+
(0, 0, 1, 1), # single pixel, top-left
52+
(99, 0, 100, 1), # single pixel, top-right
53+
(0, 99, 1, 100), # single pixel, bottom-left
54+
(99, 99, 100, 100), # single pixel, bottom-right
55+
(0, 0, 100, 1), # full top row
56+
(0, 99, 100, 100), # full bottom row
57+
(0, 0, 1, 100), # full left column
58+
(99, 0, 100, 100), # full right column
59+
(40, 40, 60, 60), # centred block
60+
),
61+
)
62+
def test_bbox_edges(mode: str, box: tuple[int, int, int, int]) -> None:
63+
im = Image.new(mode, (100, 100), 0)
64+
bands = Image.getmodebands(mode)
65+
im.paste(255 if bands == 1 else (255,) * bands, box)
66+
assert im.getbbox() == box
67+
68+
4769
@pytest.mark.parametrize("mode", ("RGBA", "RGBa", "La", "LA", "PA"))
4870
def test_bbox_alpha_only_false(mode: str) -> None:
4971
im = Image.new(mode, (100, 100))

0 commit comments

Comments
 (0)