Files
invoice-master-poc-v2/packages/shared/shared/bbox/expander.py
Yaojia Wang ad5ed46b4c WIP
2026-02-11 23:40:38 +01:00

36 lines
1005 B
Python

"""
BBox Expander Module.
Expands bounding boxes by a uniform pixel padding on all sides,
clamped to image boundaries. No field-specific or directional logic.
"""
from .scale_strategy import UNIFORM_PAD
def expand_bbox(
bbox: tuple[float, float, float, float],
image_width: float,
image_height: float,
pad: int = UNIFORM_PAD,
) -> tuple[int, int, int, int]:
"""Expand bbox by uniform pixel padding, clamped to image bounds.
Args:
bbox: (x0, y0, x1, y1) in pixels.
image_width: Image width for boundary clamping.
image_height: Image height for boundary clamping.
pad: Uniform pixel padding on all sides (default: UNIFORM_PAD).
Returns:
Expanded bbox (x0, y0, x1, y1) as integers, clamped to image bounds.
"""
x0, y0, x1, y1 = bbox
nx0 = max(0, int(x0 - pad))
ny0 = max(0, int(y0 - pad))
nx1 = min(int(image_width), int(x1 + pad))
ny1 = min(int(image_height), int(y1 + pad))
return (nx0, ny0, nx1, ny1)