Skip to main content

Device-aware image operations (CPU/GPU/MPS fallback) with a clean Python API for preprocessing tasks.

Project description

ImageOperations

ImageOperations is a minimal, device‑aware image ops library built on top of OpenCV and NumPy. You can call functions directly without initialization, or use a small facade class with a default device (CPU/GPU/MPS placeholder). When the requested device is unavailable, the library transparently falls back to CPU.

  • ✅ Functional API and OOP API
  • ✅ Device selection per call (device="cpu"|"gpu"|"mps")
  • ✅ Safe fallback (GPU/MPS → CPU)
  • ✅ Typed (PEP 561, py.typed)
  • ✅ Clean, modular structure (ops/, registry, device, types)
  • 🔧 Easy to extend: add new ops or backends without touching the public API

Note: MPS (Apple Metal) is reserved in ComputeDevice but currently falls back to CPU.


Installation

poetry add img-ops
# or with pip:
# pip install img-ops

Headless environments: replace opencv-python with opencv-python-headless in your environment if you don't need GUI capabilities.


Quick Start

Functional API

from img_ops import bgr_to_hsv, in_range, gauss_blur, ComputeDevice

hsv = bgr_to_hsv(img, device="gpu")  # uses GPU if CUDA available; else CPU
mask = in_range(hsv, (0, 80, 80), (10, 255, 255))  # default device (CPU)
blur = gauss_blur(img, (5, 5), 1.2, device=ComputeDevice.CPU)

OOP API with default device

from img_ops import ImageProcessor

proc = ImageProcessor(default_device="gpu")
hsv = proc.bgr_to_hsv(img)  # defaults to GPU (fallback → CPU)
mask = proc.in_range(hsv, (0, 80, 80), (10, 255, 255))  # uses proc.default_device
blur = proc.gauss_blur(img, (5, 5), 1.2, device="cpu")  # per-call override → CPU

Supported Operations (initial set)

  • Color: bgr_to_rgb, bgr_to_hsv, in_range
  • Filters: gauss_blur
  • Segmentation (CPU-only): find_contours, connected_components
  • Utils: sum_masks (internal registry op)

Add more ops by editing registry.py and adding thin wrappers in ops/.


Devices

from img_ops import ComputeDevice
# ComputeDevice.GPU  -> CUDA (if available)
# ComputeDevice.MPS  -> reserved (falls back to CPU)
# ComputeDevice.CPU  -> always available

Fallback behavior

  • If you request GPU and CUDA is unavailable, the call automatically falls back to CPU.
  • MPS is a placeholder and currently always falls back to CPU.

Types

The library ships with type hints and py.typed.

from img_ops import CvImage, PointHSV

# CvImage: NDArray[uint8 | float32]  # shape (H,W,3) or (H,W)
# PointHSV: tuple[int, int, int]     # (H, S, V)

Shape is not fixed in the type system; keep (H, W) or (H, W, 3) by convention.


Error Handling

  • Unknown device strings raise ValueError with allowed values.
  • Missing implementations raise NotImplementedError (after attempting CPU fallback).

Extending the Library

Add a new operation

  1. Implement CPU/GPU/MPS callables in registry.py under _cpu(), _gpu(), _mps().
  2. Add a thin function wrapper in ops/<category>.py that normalizes device and calls dispatch(...).
  3. (Optional) Expose it in ops/__init__.py and the package __init__.py.

Add a new device

  1. Extend ComputeDevice (e.g., OPENCL, TORCH).
  2. Add a registry builder (e.g., _opencl()), wire it in REGISTRY.
  3. Add availability checks in device.py and normalization in each ops/*.py wrapper.

This keeps public API stable while evolving internals.


Performance Notes

  • GPU helps mostly for larger images or heavy kernels; small images can be faster on CPU due to upload/download overhead.
  • Prefer cv2.add over a + b for masks to handle saturation properly.

Testing

poetry run pytest
poetry run mypy src/img_ops
poetry run ruff check .

Author

Telegram GitHub

Project: https://github.com/omigutin/img_ops

Project Tracker: https://github.com/users/omigutin/projects/4

Contact: migutin83@yandex.ru


License

MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

img_ops-0.1.1.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

img_ops-0.1.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file img_ops-0.1.1.tar.gz.

File metadata

  • Download URL: img_ops-0.1.1.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.0 CPython/3.10.11 Windows/10

File hashes

Hashes for img_ops-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0be5c9c034fbc36a70d3cb922c47465f1f4c68e1a24148efe54ac524ac92eac1
MD5 35ed76e7cb25578b527e52f3eac8ef5e
BLAKE2b-256 042261a79110ad3656ae7181a6abe18dc87e8787ffb529df2855730ef63d5a1d

See more details on using hashes here.

File details

Details for the file img_ops-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: img_ops-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.0 CPython/3.10.11 Windows/10

File hashes

Hashes for img_ops-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d8dd467c9764b5e8acffa6f6038ebd3456cdbe5bfd2a64967e6b4d2b40eab2dd
MD5 ebae040f6e93122ce5a7c319f67b00b6
BLAKE2b-256 d5f75a802bda688462e0688ca53c5acd1d3492b82035f7cb54313539a66b1225

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page