Skip to main content

BoxTrail

Move bounding boxes between original and letterboxed image coordinates — without guessing the padding.

PyPI version Python versions PyPI downloads License: AGPL-3.0-only Runtime dependencies: 0

BoxTrail is a tiny, pure-Python, zero-runtime-dependency library for reconstructing bounding-box coordinates after a resize-and-pad (letterbox) operation. It exposes the actual integer resize geometry, the padding and the per-axis scales so results are inspectable and reproducible.

Version: 0.1.0. The release workflow uses PyPI Trusted Publishing (OIDC). The PyPI badges above show publication status directly from package-index services.

Why it exists

Computer-vision models often take square inputs. A 1920 × 1080 image may be resized to 640 × 360 and padded to 640 × 640. Predictions in the 640 × 640 canvas must be mapped back to the original image. Getting padding order, rounding, or width/height conventions wrong can shift boxes. BoxTrail handles this one task and deliberately does not resize images, run models, or provide annotation tooling.

Installation

pip install boxtrail

To install directly from the source repository instead:

pip install git+https://github.com/edujbarrios/boxtrail.git

Quick start

from boxtrail import letterbox

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

# A detection in the 640 x 640 model input (xyxy pixel-edge coordinates)
box = (128, 208, 400, 352)
print(transform.to_source(box))
# (384.0, 204.0, 1200.0, 636.0)

# Move a source-space box into the model input canvas
print(transform.to_target((384, 204, 1200, 636)))
# (128.0, 208.0, 400.0, 352.0)

Inspect the transform

print(transform.resized_size)  # (640, 360)
print(transform.padding)       # (0, 140, 0, 140), left/top/right/bottom
print(transform.scale)         # (1/3, 1/3) approximately
print(transform.content_box)   # (0.0, 140.0, 640.0, 500.0)
print(transform.as_dict())     # JSON-serializable geometry

When detections overlap padding

By default, boxes outside the valid image/content region raise ValueError. Set clip=True to intersect them with the valid region. A box entirely inside padding has no meaningful source coordinates and raises even with clipping.

print(transform.to_source((20, 100, 100, 200), clip=True))
# (60.0, 0.0, 300.0, 180.0)

Resize behavior

# Keep a small image at its original size, pad to the requested canvas.
t = letterbox((20, 10), (100, 100), scale_up=False)
print(t.padding)   # (40, 45, 40, 45)

# Or anchor it to the upper-left corner instead of centering it.
t = letterbox((20, 10), (100, 100), scale_up=False, center=False)
print(t.padding)   # (0, 0, 80, 90)

Conventions and guarantees

  • Sizes: (width, height); boxes: (x1, y1, x2, y2) with continuous pixel-edge coordinates and strictly positive width/height.
  • Padding: (left, top, right, bottom) in integer pixels.
  • Resize: preserve aspect ratio; calculate ratio = min(target_w/source_w, target_h/source_h), capped at 1 when scale_up=False. Each resized dimension uses Python's round() (ties to even). Any odd padding remainder goes to right/bottom when centered.
  • Actual scale: computed separately for x and y from the rounded resized dimensions; this matters when integer rounding slightly changes the aspect ratio.
  • Bounds: strict by default with a tiny floating-point tolerance; use clip=True for an explicit intersection. Non-finite, reversed, zero-area or non-intersecting boxes raise ValueError.
  • Round-trip: coordinate mapping is invertible within the unpadded image content up to floating-point precision; lost image pixels or image interpolation cannot be reversed.

Important: BoxTrail does not operate on the image itself. The actual model preprocessing must use exactly the same resize dimensions, rounding and padding convention. If your pipeline uses stride alignment, a different rounding rule, cropping, stretched resize, or automatic padding, the returned geometry does not describe that pipeline. In v0.1.0 only the xyxy format and letterbox() are supported.

Run tests

python -m unittest discover -s tests -v

There are no third-party runtime or test dependencies. Release automation runs the tests, builds distributions, then publishes through the official PyPA action and GitHub OIDC Trusted Publishing; see docs/RELEASING.md.

License, copyleft and attribution

GNU Affero General Public License v3.0 only (AGPL-3.0-only). Copyright (C) 2026 Eduardo J. Barrios (@edujbarrios). See LICENSE and NOTICE. Preserve applicable copyright, licensing and attribution notices when conveying covered code. Modified versions and certain network-service uses carry source-code obligations under the AGPL. Commercial use is not prohibited. You should review the license before integrating the library into proprietary projects.

Contributions are welcome under the same license; see CONTRIBUTING.md.

Metadata

Release files for boxtrail 0.1.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.1.0
File Size Uploaded Provenance Actions
boxtrail-0.1.0.tar.gz 19.2 kB With provenance Details for boxtrail-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 38.2 kB

Release files / boxtrail-0.1.0.tar.gz

Download URL boxtrail-0.1.0.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
ad5ab0e66e630bd43d425c5906872b27d8bd7f47528af5260f2fa4a416d14264
BLAKE2b-256 checksum
How to use checksums
2999a7e6095f4aed405d943cb174f3d52cd2eb884f3eab524eb4d77f3519e5d3
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.1.0-py3-none-any.whl

Download URL boxtrail-0.1.0-py3-none-any.whl
Size 19.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
72d89d999dfd0f39d59442729918d46c1a329f66220eb23aa1c9c8097de8800e
BLAKE2b-256 checksum
How to use checksums
cf18166f233d29a17b7fa05c9e152a7474a9af7eed265d9ef518875751ea1fa4
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.1.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.1.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