spacr.image_colors

Colour-order boundaries for OpenCV.

spaCR’s in-memory colour contract is RGB (or RGBA). OpenCV is the one major dependency that decodes and encodes three-channel images as BGR, so conversions belong directly beside imread/imwrite rather than in plotting or model code. Grayscale arrays pass through unchanged.

Functions

cv2_to_rgb(→ Optional[numpy.ndarray])

Convert an OpenCV BGR/BGRA array to spaCR's RGB/RGBA contract.

read_image_rgb(→ Optional[numpy.ndarray])

Read with OpenCV and immediately return RGB/RGBA in memory.

rgb_to_cv2(→ numpy.ndarray)

Convert an RGB/RGBA array only for an immediate OpenCV write call.

write_image_rgb(→ bool)

Write an RGB/RGBA array through OpenCV without leaking BGR internally.

Module Contents

spacr.image_colors.cv2_to_rgb(image: numpy.ndarray | None) → numpy.ndarray | None[source]

Convert an OpenCV BGR/BGRA array to spaCR’s RGB/RGBA contract.

Parameters:

image – decoded OpenCV array, grayscale array, or None.

Returns:

contiguous RGB/RGBA data for three- or four-channel inputs; None, grayscale, and other channel counts pass through unchanged.

spacr.image_colors.read_image_rgb(path: PathValue, flags: int = -1) → numpy.ndarray | None[source]

Read with OpenCV and immediately return RGB/RGBA in memory.

Parameters:
  • path – image path accepted by OpenCV.

  • flags – OpenCV read mode; -1 preserves the stored dtype and alpha channel.

Returns:

RGB/RGBA or grayscale image data, or None when OpenCV cannot read the path.

spacr.image_colors.rgb_to_cv2(image: numpy.ndarray) → numpy.ndarray[source]

Convert an RGB/RGBA array only for an immediate OpenCV write call.

Parameters:

image – spaCR RGB/RGBA array, or a grayscale array to pass through.

Returns:

contiguous BGR/BGRA data for three- or four-channel inputs; grayscale and other channel counts pass through unchanged.

spacr.image_colors.write_image_rgb(path: PathValue, image: numpy.ndarray, params=None) → bool[source]

Write an RGB/RGBA array through OpenCV without leaking BGR internally.

Parameters:
  • path – output image path passed to OpenCV.

  • image – spaCR RGB/RGBA array, converted immediately before encoding.

  • params – optional OpenCV encoder parameter sequence.

Returns:

whether OpenCV encoded and wrote the image successfully.