Skip to main content

BoxTrail

Lightweight, auditable bounding-box coordinate transformations for letterboxed images.

PyPI Python CI License: AGPL-3.0-only

BoxTrail maps bounding boxes between original-image and model-input coordinates, using explicit resize and padding geometry. It supports xyxy, xywh, and cxcywh representations, absolute or normalized coordinates, and lists of detections. No runtime dependencies are required.

BoxTrail does not resize image pixels, run inference, or draw annotations. It works alongside your existing preprocessing pipeline.

Install

pip install boxtrail

Python 3.10 or newer is required. NumPy arrays can be passed as inputs if NumPy is installed, but NumPy is not a dependency.

Basic usage

from boxtrail import letterbox

t = letterbox(source=(1920, 1080), target=(640, 640))

# Absolute xyxy detection in model coordinates:
print(t.to_source((128, 208, 400, 352)))
# (384.0, 204.0, 1200.0, 636.0)

# Convert from original image coordinates to model coordinates:
print(t.to_target((384, 204, 1200, 636)))
# (128.0, 208.0, 400.0, 352.0)

For this example, a 1920 × 1080 image is resized to 640 × 360, then padded 140 pixels on the top and bottom.

Work with YOLO-style normalized boxes

A YOLO-style detection often uses normalized cxcywh: center x, center y, width and height, each relative to the whole model input canvas, including padding.

from boxtrail import letterbox

t = letterbox((1920, 1080), (640, 640))

# Center, width and height, normalized to 640 x 640.
yolo_box = (0.4125, 0.4375, 0.425, 0.225)

# Convert into pixel-edge xyxy in the original 1920 x 1080 image:
original = t.to_source(
    yolo_box,
    input_format="cxcywh",
    input_normalized=True,
)
print(original)
# (384.0, 204.0, 1200.0, 636.0)

These conversions are explicit: by default the output remains absolute xyxy. If you need a normalized output too, specify output_format and output_normalized.

Use actual preprocessing geometry

For production pipelines, prefer from_geometry() with the dimensions and padding recorded after actual preprocessing, rather than predicting them from the image sizes.

from boxtrail import from_geometry

t = from_geometry(
    source=(100, 50),
    resized=(200, 100),
    padding=(10, 20, 30, 40),  # left, top, right, bottom
)

# Absolute top-left x,y,width,height -> original-image xyxy:
print(t.to_source((30, 30, 60, 40), input_format="xywh"))
# (10.0, 5.0, 40.0, 25.0)

# Original-image xyxy -> normalized YOLO-style center coordinates:
print(t.to_target(
    (10, 5, 40, 25),
    output_format="cxcywh",
    output_normalized=True,
))
# (0.25, 0.3125, 0.25, 0.25)

Using from_geometry() avoids assumptions about rounding rules, padding distribution, and nonuniform integer scaling. You must supply measurements consistent with your actual preprocessing.

Multiple boxes

boxes = [
    (30, 30, 60, 40),
    (10, 20, 200, 100),
]

mapped = t.to_source_many(
    boxes,
    input_format="xywh",
    clip=True,
)

# Convert back to model coordinates using normalized cxcywh:
normalized = t.to_target_many(
    mapped,
    output_format="cxcywh",
    output_normalized=True,
)

The _many methods preserve input order and return list[tuple[float, float, float, float]]. If an input is invalid, ValueError identifies the first invalid index. The methods accept iterables and optional NumPy arrays shaped (N, 4).

Coordinate conventions

Option Input or output values
xyxy (default) (x1, y1, x2, y2): two corners
xywh (x, y, width, height): top-left corner and dimensions
cxcywh (center_x, center_y, width, height)
input_normalized=True Input values measured in fractions of the input coordinate space
output_normalized=True Output values measured in fractions of the output coordinate space

Normalization uses source image size for source-space boxes and full target canvas size, including padding for target-space boxes. This applies separately to both directions.

Dimensions are always (width, height). Boxes have positive width and height; coordinates are continuous pixel edges, not indices. Input boxes crossing image bounds or padding raise ValueError by default. Pass clip=True to intersect the box with valid image content; a completely disjoint box still raises an error.

Full API: optional keyword arguments

All to_source(), to_target(), to_source_many(), and to_target_many() methods accept:

clip=False
input_format="xyxy"
output_format="xyxy"
input_normalized=False
output_normalized=False

The defaults preserve the API and output conventions of earlier BoxTrail versions. Output coordinates are floating-point numbers.

Geometry, scope and limitations

  • letterbox() describes aspect-ratio-preserving contain resizing with Python's round() for integer dimensions and centered padding (any odd remainder goes right/bottom). Use scale_up=False to avoid enlargement and center=False to anchor top-left.
  • from_geometry() records exact integer resized dimensions and (left, top, right, bottom) padding, including asymmetric padding.
  • Geometry inspection: t.source_size, t.target_size, t.resized_size, t.padding, t.scale, t.content_box and t.as_dict().
  • Coordinate mapping round-trips within floating-point precision when no clipping occurs. Clipped coordinates and image pixels cannot be reconstructed.
  • Not supported: rotated boxes, polygons, perspective transforms, cropping, stride padding inferred automatically from dimensions, or conversion of the image pixels themselves.
  • If your preprocessing uses different rounding/padding, letterbox() alone is not sufficient: use measured from_geometry().

Development and testing

git clone https://github.com/edujbarrios/boxtrail.git
cd boxtrail
python -m pip install -e .
python -m unittest discover -s tests -v

CI tests Python 3.10–3.14 and optional NumPy input interoperability. Automated PyPI publishing occurs only when both package version references are updated to a new version and merged into main; see the release guide.

License and attribution

Copyright © 2026 Eduardo J. Barrios (@edujbarrios).

BoxTrail is distributed under GNU Affero General Public License v3.0 only (AGPL-3.0-only). Covered redistribution requires preservation of applicable copyright and license notices and compliance with AGPL copyleft requirements. Commercial use is allowed under the license's conditions. See LICENSE and NOTICE. Contributions are welcome under the same license; see CONTRIBUTING.md.

Metadata

Release files for boxtrail 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for boxtrail 0.3.0
File Size Uploaded Provenance Actions
boxtrail-0.3.0.tar.gz 25.8 kB With provenance Details for boxtrail-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for boxtrail 0.3.0
File Interpreter ABI Platform Actions
boxtrail-0.3.0-py3-none-any.whl Python 3 none any Details for boxtrail-0.3.0-py3-none-any.whl

Total release size: 46.7 kB

Release files / boxtrail-0.3.0.tar.gz

Download URL boxtrail-0.3.0.tar.gz
Size 25.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5954dc19403101545c54e4961f6687098b504575c2fa9a0ae148513aae3a606a
BLAKE2b-256 checksum
How to use checksums
4b84c30f30a7e43388b2793836bde35680d8e670dfa21b56725782c84d09488c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Release files / boxtrail-0.3.0-py3-none-any.whl

Download URL boxtrail-0.3.0-py3-none-any.whl
Size 20.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
70d0b761d2be7ca4b28d1cefb4d41348ca9634fa3ea39c438d45c7d2c6c30417
BLAKE2b-256 checksum
How to use checksums
2df88e0db3e79c5d8232ac4934d2fbdd8bc6cf30ff986c66de7b2d55b6255021
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Security for boxtrail 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for boxtrail-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page