Color Operations
The `color-operations` library applies basic color-oriented image operations, serving as a modified fork of Mapbox's `rio-color` library. It specifically removes the `rasterio` dependency and ensures compatibility with Python 3.10 and newer. Version 0.2.0 was released recently, focusing on distribution and Python version updates.
Common errors
-
ValueError: operands could not be broadcast together with shapes (H,W,C) (C,H,W)
cause The input NumPy array has an incorrect dimension order. `color-operations` expects `(bands, rows, columns)`, but you might be providing `(rows, columns, bands)`.fixReshape your NumPy array. If your array is `(rows, columns, bands)`, use `your_array.transpose(2, 0, 1)` before passing it to `color-operations` functions. -
Output image appears completely washed out, over-saturated, or incorrect.
cause Input pixel values are not scaled between 0.0 and 1.0 (e.g., still 0-255), or the output 0-1.0 values are being misinterpreted by a display tool expecting 0-255.fixVerify that your input NumPy array's pixel values are floating-point numbers between 0.0 and 1.0. If starting from an 8-bit image (0-255), convert it with `image_255.astype(np.float32) / 255.0`. Remember to convert output back to 0-255 for display/saving if necessary (e.g., `(output_array * 255).astype(np.uint8)`). -
ModuleNotFoundError: No module named 'rasterio'
cause You are attempting to use `color-operations` in a manner that expects `rasterio` integration. This library is a fork specifically designed to *remove* `rasterio` as a dependency.fixThis library does not support `rasterio` directly. Extract the raw `numpy.ndarray` from your `rasterio` dataset to use with `color-operations`. If deep `rasterio` integration is required, use the original `rio-color` library.
Warnings
- breaking Python 3.8 is no longer supported; the library now requires Python >= 3.9 for installation and use.
- breaking This library is an explicit fork of `rio-color` that *removes* the `rasterio` dependency. Code expecting `rasterio` objects or direct `rasterio` integration will not work.
- gotcha Input and output NumPy arrays for all operations are expected to be in `(bands, columns, rows)` order (similar to `rasterio`'s internal representation) and have pixel values scaled from 0.0 to 1.0. Many other image processing libraries use `(rows, columns, bands)` or 0-255 scaling.
Install
-
pip install color-operations
Imports
- operations
from color_operations import operations
Quickstart
import numpy as np
from color_operations import operations
# Simulate a 3-band RGB image (e.g., 100x100 pixels)
# Input arrays are expected to be (bands, rows, columns) and scaled 0-1.
image_array = np.random.rand(3, 100, 100).astype(np.float32)
# Apply a sigmoidal contrast adjustment
# contrast: adjusts the slope of the sigmoid function (higher values mean higher contrast)
# bias: shifts the sigmoid function (affects brightness)
sigmoidal_adjusted = operations.sigmoidal(image_array, contrast=5, bias=0.5)
# Apply gamma correction to brighten midtones (e.g., gamma > 1.0 darkens, < 1.0 brightens)
gamma_corrected = operations.gamma(image_array, g=0.8)
# Adjust saturation (proportion > 1.0 increases saturation, < 1.0 decreases)
saturated_image = operations.saturation(image_array, proportion=1.5)
print(f"Original image array shape: {image_array.shape}")
print(f"Sigmoidal adjusted image shape: {sigmoidal_adjusted.shape}")
print(f"Gamma corrected image shape: {gamma_corrected.shape}")
print(f"Saturated image shape: {saturated_image.shape}")