Skip to content

Commit 87181ef

Browse files
committed
Add bbox benchmark
1 parent 8a37d87 commit 87181ef

1 file changed

Lines changed: 44 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) # zero-filled background
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: # "corner": push content to the far edge so the top/left scans are long
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", "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)

0 commit comments

Comments
 (0)