Skip to main content

sdimg

Small, function-based image and mask processing library built on numpy.ndarray. The public API is pure functions and requires Python 3.12+.

Install

pip install sdimg

Modules

  • sdimg.image: image conversion, filtering, enhancement, normalization, deterministic array IDs, lossless string codecs, and file I/O.
  • sdimg.mask: binary conversion, geometry, connected components, morphology, hulls, and distance transforms.
  • sdimg.spatial: crop, pad, resize, rotate, flip, and tile split/merge.
  • sdimg.segment: Otsu thresholding and GrabCut refinement.

Public imports are flat within each domain:

from sdimg.image import apply_gaussian_blur, equalize_histogram
from sdimg.mask import apply_morphology
from sdimg.segment import refine_grabcut

The implementation keeps representation-specific responsibilities inside their domains: image processing owns visual/alpha channel handling, spatial operations own dtype restoration, and segmentation owns its bbox and ROI lifecycle. Import public functions from the flat domain packages rather than implementation modules.

Core Contracts

  • Inputs are numpy.ndarray values with real numeric or boolean dtype.
  • Images use non-empty shape (H, W) or (H, W, C) with C in {1, 2, 3, 4}.
  • Color images are RGB. Convert OpenCV BGR inputs before calling sdimg.
  • Channel meanings are grayscale, grayscale+alpha, RGB, and RGBA.
  • sdimg.image processing returns np.uint8. Filters and enhancement preserve channel shape and leave alpha content unchanged.
  • Masks use non-empty shape (H, W) and values bool, {0, 1}, or {0, 255}. Mask processing returns binary np.uint8 in {0, 1}; distance transforms return np.float32.
  • sdimg.spatial operations preserve input dtype and channel shape.
  • refine_grabcut accepts an image-sized binary mask and returns a binary mask with the same spatial shape. It derives and restores its ROI internally.
  • Points use (x, y). Bounding boxes use (xmin, ymin, xmax, ymax), minimum-inclusive and maximum-exclusive.
  • Empty mask geometry returns None where no geometry exists.

Error Policy

  • TypeError: wrong Python input type.
  • ValueError: invalid shape, dtype, parameter, mask value, or bbox.
  • RuntimeError: wrapped OpenCV, Pillow, or third-party failure.

Quick Example

import numpy as np

from sdimg.image import apply_gaussian_blur, equalize_histogram
from sdimg.mask import apply_morphology, extract_roi
from sdimg.segment import refine_grabcut

image = np.random.default_rng(0).integers(
    0,
    256,
    (128, 128, 3),
    dtype=np.uint8,
)
mask = np.zeros((128, 128), dtype=np.uint8)
mask[32:96, 40:88] = 1

image = equalize_histogram(image)
image = apply_gaussian_blur(image, kernel_size=(5, 5), sigma_x=1.2)
mask = apply_morphology(mask, operation="open", kernel_size=(3, 3))

refined = refine_grabcut(image, mask)

Supporting Image Utilities

from sdimg.image import (
    decode_image,
    encode_image,
    make_array_id,
    read_image,
    write_image,
)

image = read_image("input.tif")
image_id = make_array_id(image, prefix="img_")

payload = encode_image(image)
restored = decode_image(payload)
write_image(f"{image_id}.png", restored)

read_image returns RGB uint8. High-bit-depth and floating images are scaled explicitly before RGB conversion. write_image writes RGB and ignores alpha. The lossless WebP string codec preserves RGBA alpha and ignores grayscale+alpha's alpha channel.

Metadata

Release files for sdimg 0.3.1

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

Source distribution (sdist)

Source distribution for sdimg 0.3.1
File Size Uploaded
sdimg-0.3.1.tar.gz 18.1 kB Details

Built distribution (wheel)

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

Total release size: 43.3 kB

Release files / sdimg-0.3.1.tar.gz

Download URL sdimg-0.3.1.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a0e67bda5570db42efb1df72b58a7292ab8ff5a9be3dfba5b5de803173b2ce05
BLAKE2b-256 checksum
How to use checksums
6abe0953b2b30e336558caf5ee6ef308222a853539a0dfb85ce593b2b2d2432d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / sdimg-0.3.1-py3-none-any.whl

Download URL sdimg-0.3.1-py3-none-any.whl
Size 25.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7aabdb7bd2a1bce83154014a2e5f28151ace8120dc2cd7e2898a49964c677f89
BLAKE2b-256 checksum
How to use checksums
dbcf2c9eb7502074d4f4126a230343b8972d5b7c94f2712e3607286535fac8a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

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