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.
poetry add img-ops
# or with pip:
# pip install img-opsHeadless environments: replace opencv-python with opencv-python-headless in your environment
if you don't need GUI capabilities.
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)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- 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/.
from img_ops import ComputeDevice
# ComputeDevice.GPU -> CUDA (if available)
# ComputeDevice.MPS -> reserved (falls back to CPU)
# ComputeDevice.CPU -> always available- If you request
GPUand CUDA is unavailable, the call automatically falls back toCPU. MPSis a placeholder and currently always falls back toCPU.
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.
- Unknown device strings raise
ValueErrorwith allowed values. - Missing implementations raise
NotImplementedError(after attempting CPU fallback).
- 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.
- 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.
- 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.
poetry run pytest
poetry run mypy src/img_ops
poetry run ruff check .Project: https://github.com/omigutin/img_ops
Project Tracker: https://github.com/users/omigutin/projects/4
Contact: migutin83@yandex.ru
MIT License. See LICENSE for details.