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
ComputeDevicebut 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 convert_bgr_to_hsv, in_range, gauss_blur, ComputeDevice
hsv = convert_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.convert_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:
convert_bgr_to_rgb,convert_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
GPUand CUDA is unavailable, the call automatically falls back toCPU. MPSis a placeholder and currently always falls back toCPU.
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
ValueErrorwith allowed values. - Missing implementations raise
NotImplementedError(after attempting CPU fallback).
Extending the Library
Add a new operation
- Implement CPU/GPU/MPS callables in
registry.pyunder_cpu(),_gpu(),_mps(). - Add a thin function wrapper in
ops/<category>.pythat normalizesdeviceand callsdispatch(...). - (Optional) Expose it in
ops/__init__.pyand the package__init__.py.
Add a new device
- Extend
ComputeDevice(e.g.,OPENCL,TORCH). - Add a registry builder (e.g.,
_opencl()), wire it inREGISTRY. - Add availability checks in
device.pyand normalization in eachops/*.pywrapper.
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.addovera + bfor masks to handle saturation properly.
Testing
poetry run pytest
poetry run mypy src/img_ops
poetry run ruff check .
Author
Project: https://github.com/omigutin/img_ops Project Tracker: ttps://github.com/users/omigutin/projects/4 Contact: migutin83@yandex.ru
License
MIT License. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file img_ops-0.1.0.tar.gz.
File metadata
- Download URL: img_ops-0.1.0.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
789b1c852d738034b879548d3661276cd79defe5b36af050cec2148d732c2733
|
|
| MD5 |
ec7060d95080475e56319420a3095ea6
|
|
| BLAKE2b-256 |
21d29a2dc2a806b338e036a54329594362654f69a4a177198abf270478737410
|
File details
Details for the file img_ops-0.1.0-py3-none-any.whl.
File metadata
- Download URL: img_ops-0.1.0-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
269dffa9d1b6f11d459a7497a83aa01a6c6a3759ad831830dbf2767f53598393
|
|
| MD5 |
a57efefa45ed8b349a84d38e5fbd8d09
|
|
| BLAKE2b-256 |
1996f32a32a689fce7701f3ee115e42287a08d2e372e0bda673d0dd0c1b63faf
|