Skip to main content

raygeo.image.convert

Functions

rgba_to_binary()

rgba_to_binary(
rgba: numpy.NDArray[numpy.uint8],
width: int,
height: int,
stride: int,
threshold: int = 128,
invert: bool = False,
) -> numpy.NDArray[numpy.uint8]

Convert raw BGRA pixel buffer to binary image using thresholding.

Transparent pixels (alpha == 0) are always treated as white (0).

ParameterTypeDescription
rgbanumpy.NDArray[numpy.uint8]Flattened uint8 buffer of shape (stride * height * 4,).
widthintImage width in pixels.
heightintImage height in pixels.
strideintRow stride in pixels.
thresholdint = 128Brightness value (0-255) for binarization.
invertbool = FalseIf True, pixels above threshold become black (1).
Returnsnumpy.NDArray[numpy.uint8]2D binary uint8 array (values 0 or 1) with shape (height, width).
ComplexityO(w*h)

rgba_to_grayscale()

rgba_to_grayscale(
rgba: numpy.NDArray[numpy.uint8],
width: int,
height: int,
stride: int,
) -> tuple[numpy.NDArray[numpy.uint8], numpy.NDArray[numpy.float32]]

Convert raw BGRA pixel buffer to grayscale with alpha unpremultiplication.

Performs proper unpremultiplication of alpha and blends to white background for grayscale calculation using BT.601 luminance weights.

ParameterTypeDescription
rgbanumpy.NDArray[numpy.uint8]Flattened uint8 buffer of shape (stride * height * 4,).
widthintImage width in pixels.
heightintImage height in pixels.
strideintRow stride in pixels (may be larger than width).
Returnstuple[numpy.NDArray[numpy.uint8], numpy.NDArray[numpy.float32]]Tuple of (grayscale_uint8, alpha_float32) arrays, each (height, width).
ComplexityO(w*h)

rgba_to_grayscale_inplace()

rgba_to_grayscale_inplace(
rgba: numpy.NDArray[numpy.uint8],
width: int,
height: int,
stride: int,
) -> None

Convert raw BGRA pixel buffer to grayscale in place.

Modifies the buffer directly, converting BGR channels to grayscale while preserving the alpha channel.

ParameterTypeDescription
rgbanumpy.NDArray[numpy.uint8]Flattened uint8 buffer of shape (stride * height * 4,).
widthintImage width in pixels.
heightintImage height in pixels.
strideintRow stride in pixels.
ReturnsNone
ComplexityO(w*h)