smart-image-splitter · High-Precision Rust + Python Image Splitter
English | 简体中文
smart-image-splitter is a high-performance, pixel-accurate composite image segmentation Python package powered by a native Rust (PyO3 + Rayon) core and a full-parity vectorized OpenCV / NumPy fallback engine.
It is engineered for real-world composite images where naive W / N × H / M grid slicing fails—including slide sheets with misaligned seams, asymmetric Bento layouts, white-guttered collages, scattered objects on solid/transparent backgrounds, and overlapping photo grids (via optional ML segmentation hooks).
Table of Contents
- Why Naive Grid Slicing Fails
- Algorithm Theory & Mathematical Formulation
- 2.1 Directional Step + 1px Hairline Energy Profiling
- 2.2 Non-Linear Coverage Weighting & Tangential Smoothness Penalty
- 2.3 Gutter Valley Detection & Contiguous Run Collapsing
- 2.4 Recursive Hierarchical XY-Cut BSP & Sibling Grid-Line Propagation
- 2.5 Per-Cell Discontinuity Snap & Bleed Decontamination
- 2.6 Separable O(N) DSU Contour Segmentation & 1:1 Tight Edge Refinement
- Engineering Practice & Extreme Performance Optimization
- Quick Start (
pip installDirectly from GitHub) - Repository Structure
1. Why Naive Grid Slicing Fails
Consider the included 2848 × 1600 3×3 presentation slide sheet (examples/sample_slides.png):
- A uniform 3×3 mathematical grid slices at
x = 949, 1898andy = 533, 1066. - In reality, the true panel boundaries sit at:
- Horizontal seams:
y = 531,y = 1043..1047 - Left column seam:
x = 945..946(with a1pxgray divider(213, 214, 217)atx = 946) - Right column seam:
x = 1824in Rows 0–1, shifting 12px left tox = 1812in Row 2!
- Horizontal seams:
- Naive uniform slicing produces up to 86 pixels of cross-panel bleed and splits slide content down the middle.
- Worse yet, adjacent white-background slides (e.g., Row 1 Col 0 vs. Row 2 Col 0) are separated only by a faint
1pxshadow hairline—invisible to naive thresholding, yet crisp when modeled with a combined step + impulse gradient and sibling grid-line propagation.
2. Algorithm Theory & Mathematical Formulation
2.1 Directional Step + 1px Hairline Energy Profiling
For any candidate axis position $p$ (e.g., row $y$ with orthogonal samples $x \in [x_0, x_1)$), a true seam exhibits either:
- A color step transition across two neighboring regions, or
- A
1pxhairline divider / drop shadow where pixel $I(p)$ deviates sharply from both neighbors $I(p-1)$ and $I(p+1)$ (even when both panels share the exact same white background!).
We compute two $L_1$ color-space responses at each sample point $u$:
$$E_{\text{step}}(p, u) = |I(p, u) - I(p+1, u)|_1$$
$$E_{\text{line}}(p, u) = \frac{1}{2} |2I(p, u) - I(p-1, u) - I(p+1, u)|_1$$
Total local edge energy at $(p, u)$ is:
$$E(p, u) = E_{\text{step}}(p, u) + E_{\text{line}}(p, u)$$
Why this matters in practice:
- If Slide A (navy) meets Slide B (white), $E_{\text{step}}$ fires strongly ($\approx 600$).
- If Slide A (white) meets Slide B (white) across a
1pxlight-gray shadow line $\Delta = 15$ per channel, $E_{\text{step}} \approx 45$ and $E_{\text{line}} \approx 45$, reinforcing each other while suppressing uniform interior noise.
2.2 Non-Linear Coverage Weighting & Tangential Smoothness Penalty
Text, charts, and tables inside a panel also create strong local gradients—how do we avoid cutting through a chart title or table header?
-
$\pm 2\text{px}$ Neighborhood Coverage $C(p)$: Real layout seams are rarely strictly horizontal to
0pxunder JPEG/anti-aliasing compression; they may wobble by $\pm 1\text{–}2\text{px}$. A sample $u$ is marked active if any row/column within $[p-2, p+2]$ satisfies $E(r, u) > 16$: $$C(p) = \frac{1}{N} \sum_{u=1}^{N} \mathbb{I}!\left[\max_{r \in [p-2, p+2]} E(r, u) > 16\right]$$ Text lines rarely span $> 70%$ of the full panel width, whereas true panel boundaries have $C(p) \in [0.85, 1.00]$. Applying the non-linear power $C(p)^{2.4}$ strongly attenuates partial interior lines (e.g., $0.4^{2.4} = 0.11$ vs. $0.95^{2.4} = 0.88$). -
Tangential Smoothness Penalty $S_{\text{tan}}(p)$: Along a true divider or clean slide edge, pixel luminance varies smoothly in the tangential direction $u$: $$S_{\text{tan}}(p) = \frac{1}{N-1} \sum_{u=2}^{N} \left| L(p, u) - L(p, u-1) \right|, \quad L = R + G + B$$ Taking the minimum smoothness across $[p-1, p, p+1]$ ensures a sharp edge right next to a clean background isn't penalized by texture on the opposite side.
-
Combined Seam Score: $$\text{Score}{\text{seam}}(p) = \frac{\left(\bar{E}{\text{step}}(p) + 0.7,\bar{E}{\text{line}}(p)\right) \cdot C(p)^{2.4}}{1 + 0.12 \cdot S{\min}(p)}$$ Candidate peaks are selected via 1D Non-Maximum Suppression (NMS) with minimum spacing $d_{\min} = \max(24, \text{minRegionRatio} \cdot \min(W, H))$ and coverage gate $C(p) \ge 0.72$.
2.3 Gutter Valley Detection & Contiguous Run Collapsing
When panels are separated by wide solid or transparent gutters (e.g., a 40px white margin between cards), edge gradients fire only on the gutter walls, not inside the empty gap.
- Border background color $\mathbf{b}$ and border uniformity $U_{\text{border}}$ are estimated via $8\times 8\times 8$-quantized histogram binning over 4 image perimeters (with alpha transparency check $\alpha < 32$).
- When $U_{\text{border}} > 0.72$, any interior strip with $> 95%$ background match, low tangential variance ($S_{\text{tan}} < 2.5$), and foreground content present on both sides receives a high gutter score: $$\text{Score}{\text{gutter}}(p) = \frac{110}{1 + S{\text{tan}}(p)}$$
- Contiguous Run Collapsing: A
40pxwide gutter generates 40 adjacent high-scoring rows/columns. Collapsing each contiguous gutter run $[i, j]$ to its single midpoint $\lfloor (i + j)/2 \rfloor$ guarantees cutting right down the center of the gutter rather than fracturing wide gutters into empty slivers. Empty background-only leaf regions ($< 3%$ foreground density) are discarded automatically.
2.4 Recursive Hierarchical XY-Cut BSP & Sibling Grid-Line Propagation
Rather than slicing globally along X and Y simultaneously:
- At each region $[x_0, y_0, x_1, y_1]$, profile both H and V seam scores.
- Select the dominant split axis (choosing H if $\max \text{Score}_H \ge 0.72 \cdot \max \text{Score}_V$, matching natural row-first slide flow).
- Partition into horizontal/vertical bands and recursively profile each sub-band independently.
- Result: Row 0–1 naturally finds
x = 1824while Row 2 independently findsx = 1812—zero global misalignment!
- Result: Row 0–1 naturally finds
- Sibling Grid-Line Propagation:
Suppose Column 1|2 has a faint white-on-white boundary below threshold $T$ on one sub-band while sibling columns above/below split cleanly around
y_cut. For every internal boundary line $p_{\text{cut}}$ produced by siblings, any adjacent leaf spanning $p_{\text{cut}}$ searches a narrow $\pm 12\text{px}$ window under relaxed thresholds ($\text{cov} \ge 0.78$, $\bar{E}{\text{raw}} \ge 10$, $S{\text{tan}} < 2.2$). This recovers low-contrast white-on-white seams with zero false positives.
2.5 Per-Cell Discontinuity Snap & Bleed Decontamination
After BSP leaves are finalized, each cell undergoes two refinement stages:
- Local $\pm 8\text{px}$ Edge Snap: Each interior edge snaps independently within $\pm 8\text{px}$ to the exact pixel transition of that specific cell (correcting minor diagonal tilt or row-by-row drift).
- Outer Margin & Anti-Aliased Bleed Decontamination (
bleed_trim):- First strips any remaining uniform canvas background rows/columns (
> 94%background match). - Then inspects up to
bleed_trim(default2px) outer pixel rows/columns along internal cut edges against an interior reference lineref = edge ± (bleed_trim + 2). Whenever the average $L_1$ difference exceeds20, the outer1pxtransition/divider pixel (e.g.,1pxgray border or JPEG chroma ringing) is peeled off—guaranteeing zero white halo on dark slides and zero dark edge bleed on light slides.
- First strips any remaining uniform canvas background rows/columns (
2.6 Separable $O(N)$ DSU Contour Segmentation & 1:1 Tight Edge Refinement
When an image consists of scattered objects, UI stickers, or transparent RGBA sprites ($U_{\text{border}} > 0.72$ and contour coverage $< 65%$ of total area with $\ge 2$ components, or explicit contour mode):
- Downsample onto a canonical $\le 720\text{px}$ grid (
scale = ceil(max(W, H) / 720)) and threshold foreground against $\mathbf{b}$ (or $\alpha > 24$). - Apply separable 1D forward/backward sweep morphological closing/dilation in $O(s_w \cdot s_h)$ time (independent of
contour_gap_px, avoiding $O(N \cdot r^2)$ nested loops). - Extract 8-connected components using two-pass Disjoint-Set Union (DSU) with path halving in Rust (
src/lib.rs) orcv2.connectedComponentsWithStatsin OpenCV (python/smart_splitter/engine.py). - 1:1 Full-Resolution Edge Tightening: Because downsampling introduces up to $\pm \text{scale}$ quantization error, every component bounding box is shrunk/expanded directly against 1:1 raw image pixels until all 4 borders sit on exact foreground boundaries (
0pxinteger error).
3. Engineering Practice & Extreme Performance Optimization
| Optimization | Problem Solved | Concrete Implementation |
|---|---|---|
1. Row-Major Parallel Scanning (Rayon) |
Vertical column loops for x { for y } stride W * C bytes per pixel read, thrashing CPU L1/L2 caches |
Inverted vertical profiling to row-major ys.par_iter().fold().reduce() in Rust so every CPU thread streams contiguous cache lines |
| 2. Single-Pass Active Tracking | Allocating O(span × 200) 2D energy matrices per recursive band wasted memory bandwidth |
Replaced 2D matrices with a monotonic 1D cursor tracker proving exact equivalence to $\max_{r \in [p-2,p+2]} E(r,u) > 16$ |
| 3. 200-Sample Orthogonal Stride | Scanning all 2,848 pixels horizontally at every row is redundant; skipping primary-axis rows misses 1px odd hairlines |
Kept primary-axis step strictly 1px while striding the orthogonal axis to $\approx 200$ samples |
| 4. Zero-Copy NumPy Borrow + GIL Release | Copying multi-megapixel NumPy buffers across FFI and blocking Python threads | Borrows C-contiguous NumPy arrays zero-copy (Cow<[u8]>) and releases the GIL via py.allow_threads(...) during detection and encoding |
| 5. Parallel In-Memory Batch Cropping | Disk I/O bottlenecks when serving slices via FastAPI or feeding PyTorch DataLoaders | batch_crop_to_memory_rs encodes all slices concurrently in RAM across Rayon worker threads without touching disk |
4. Quick Start (pip install Directly from GitHub)
Installation (Zero Extra Setup)
# Option 1: Install directly via Git + pip (recommended)
pip install git+https://github.com/<owner>/<repo>.git
# Option 2: Install via GitHub source archive URL (works even without git CLI installed)
pip install https://github.com/<owner>/<repo>/archive/refs/heads/main.zip
# Option 3: Install a prebuilt binary wheel from GitHub Releases (precompiled Rust PyO3, no compiler needed)
pip install https://github.com/<owner>/<repo>/releases/latest/download/smart_image_splitter-0.2.0-cp311-cp311-manylinux_2_17_x86_64.whl
Note on GitHub URLs &
pip: Standardpipcannot install a bare GitHub webpage URL (pip install https://github.com/owner/repo) because GitHub serves HTML there. Always usegit+https://github.com/owner/repoor/archive/refs/heads/main.zip(or a Release.whl).Automatic Rust / OpenCV Dual-Backend:
- If
cargo(Rust toolchain) is installed on the target machine,pip installautomatically compilessmart_splitter._core(PyO3+Rayon) in release mode.- If
cargois not installed,setup.pyskips Rust compilation gracefully with zero errors and uses the full-parity vectorized OpenCV / NumPy backend.- Pushing a version tag (
git tag v0.2.0 && git push --tags) triggers.github/workflows/release-wheels.yml(PyO3/maturin-action) to publish prebuilt native Rust wheels for Linux, macOS, and Windows.
CLI & Python API Usage
# CLI batch splitting
smart-split examples/sample_slides.png -o ./output_slices --mode auto --bleed-trim 2 --format png
import cv2
from smart_splitter import SmartSplitter
splitter = SmartSplitter(mode="auto", sensitivity=0.65, bleed_trim=2)
# 1. File-to-directory splitting
result, paths = splitter.split_file("examples/sample_slides.png", "output_slices/")
print(f"Split into {len(paths)} slices via {result.backend} in {result.elapsed_ms:.1f}ms")
# 2. Zero-disk in-memory splitting (FastAPI / PyTorch dataloaders)
img = cv2.imread("examples/sample_slides.png")
result, png_blobs = splitter.split_to_bytes(img, fmt="png")
# 3. Optional ML segmentation hook (SAM 2 / YOLO / ONNX) for overlapping natural photos
ml_splitter = SmartSplitter(ml_segmenter=my_sam2_box_predictor)
Local Development & Testing
pip install setuptools wheel setuptools-rust maturin pytest numpy opencv-python-headless pillow
maturin develop --release
pytest -v tests/
5. Repository Structure
.
├── Cargo.toml # Rust PyO3 + Rayon crate manifest
├── pyproject.toml # PEP 517 + setuptools / maturin build configuration
├── setup.py # Optional Rust extension builder with graceful fallback
├── MANIFEST.in # Source distribution manifest (Python + Rust sources only)
├── LICENSE / CONTRIBUTING.md
├── README.md / README.zh-CN.md
├── .github/
│ ├── workflows/
│ │ ├── ci.yml # Root pip install + Cargo + maturin + pytest CI
│ │ └── release-wheels.yml # Multi-OS prebuilt wheel publisher (Linux/macOS/Windows)
│ ├── ISSUE_TEMPLATE/bug_report.md
│ └── pull_request_template.md
├── src/
│ └── lib.rs # Native Rust (PyO3 + Rayon) engine: XY-Cut, DSU Contour & Memory Crop
├── python/
│ └── smart_splitter/
│ ├── __init__.py # Public exports (SmartSplitter, SplitBox, SplitResult)
│ ├── engine.py # Hybrid Rust / full-parity OpenCV engine & ML hook
│ └── cli.py # `smart-split` CLI entrypoint
├── tests/
│ └── test_splitter.py # Pytest unit tests (XY-Cut, White Gutter, Bleed Trim, Contour)
└── examples/
└── sample_slides.png # Real-world 2848x1600 3x3 slide benchmark image
Metadata
Release files for smart-image-splitter 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| smart_image_splitter-0.2.0.tar.gz | 43.6 kB | Without provenance | Details for smart_image_splitter-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| smart_image_splitter-0.2.0-cp313-cp313-win_amd64.whl | CPython 3.13 | CPython 3.13 | Windows x86-64 | Details for smart_image_splitter-0.2.0-cp313-cp313-win_amd64.whl |
Total release size: 766.4 kB
Release files / smart_image_splitter-0.2.0.tar.gz
| Download URL | smart_image_splitter-0.2.0.tar.gz |
|---|---|
| Size | 43.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
acd2b10e9135bfb300db749df7f061d60d055ea79698c01e08835e26a6976c64
|
|
BLAKE2b-256 checksum How to use checksums |
b654282066fc97811c8dd79b5477a0543c049fe8392ba9c5b1e1fcc7a80fa925
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.11
|
Release files / smart_image_splitter-0.2.0-cp313-cp313-win_amd64.whl
| Download URL | smart_image_splitter-0.2.0-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 722.8 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
c02a4fcdf51527e717364479d1db4a978b680cd3e79815114a150f4ac0eacd3b
|
|
BLAKE2b-256 checksum How to use checksums |
f639ccf12c2eb0334fcfb6b7408abb52c1697d6137e6e48ea6e6191d9aa8bf83
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.11
|
Security for smart-image-splitter 0.2.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.