Skip to main content

roi_rectangle

A small, typed helper for rectangular regions of interest (ROI) in NumPy images.

PyPI version Python versions Tests License Downloads

Install

pip install roi-rectangle

Quickstart

import numpy as np
from roi_rectangle import RoiRectangle

image = np.arange(100 * 120).reshape(100, 120)

roi = RoiRectangle(y1=30, y2=80, x1=20, x2=70)  # rows [30, 80), cols [20, 70)
print(roi.shape, roi.center, roi.area)          # (50, 50) (45, 55) 2500

crop = roi.slice(image)                         # same as image[30:80, 20:70]
print(crop.shape)                               # (50, 50)

roi2 = roi.recenter((60, 40)).resize(10, 20)    # methods return new ROIs
print(roi2)                                     # RoiRectangle(y1=30, y2=50, x1=55, x2=65)
print(image[roi2.get_slices()].shape)           # (20, 10)

API overview

RoiRectangle(y1, y2, x1, x2) is a dataclass. Intervals are half-open: [y1, y2) and [x1, x2). y2 or x2 may be None, meaning the region extends to the image edge on that axis. A ValueError is raised if y2 < y1 or x2 < x1.

Member Description
width, height, shape, area Size properties (shape is (height, width)); None for unbounded axes.
center (x, y) center, rounded down; None if any axis is unbounded.
move(dx, dy) New ROI shifted by (dx, dy).
recenter((x, y)) New ROI of the same size centered at (x, y).
resize(width, height) New ROI with the given size around the same center.
slice(image) image[..., y1:y2, x1:x2]; works on stacks with leading axes.
get_slices() (slice(y1, y2), slice(x1, x2)) for indexing.
to_tuple(), from_tuple(t) Convert to/from (y1, y2, x1, x2).

Coordinates are passed to NumPy unchanged: an ROI past the bottom or right edge is clipped by NumPy, while negative coordinates count from the end of the axis.

Citation

If you use this package in your work, please cite it using the metadata in CITATION.cff.

License

MIT. See LICENSE.

Metadata

Release files for roi-rectangle 1.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 roi-rectangle 1.1.0
File Size Uploaded
roi_rectangle-1.1.0.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for roi-rectangle 1.1.0
File Interpreter ABI Platform
roi_rectangle-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / roi_rectangle-1.1.0.tar.gz

Download URL roi_rectangle-1.1.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
204d87945c58d678022104f45bd25e5a93b9bc38d901ade1b5d52f68f97923e1
BLAKE2b-256 checksum
How to use checksums
46b65531f79c19ea93996a2ce610b41f1cde9adcb1f66dabee57f9a01904f302
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 15, 2026.

Transparency log

Release files / roi_rectangle-1.1.0-py3-none-any.whl

Download URL roi_rectangle-1.1.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c116a3a661f684cf42c62ebfca25ea3dfd3cab018b42fd9a2479a1852c5656ab
BLAKE2b-256 checksum
How to use checksums
ba5dd7a1501f008a43674d0cbafea47944375d6287a2ffb75f22957c9157c547
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 15, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.8

2 release files

0.0.7

2 release files

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