from_png#

static rlemasklib.RLEMask.from_png(path=None, data=None, threshold=1)[source]#

Create an RLEMask from an 8-bit grayscale PNG.

This is a fast path that avoids materializing the full pixel array, converting directly from PNG to RLE representation.

Parameters:
  • path (Union[str, PathLike, None]) – Path to PNG file (str or Path).

  • data (Union[bytes, bytearray, memoryview, None]) – PNG data as bytes, bytearray, or memoryview.

  • threshold (int) – Pixels with values >= threshold become foreground (1). Default is 1, so any nonzero pixel is foreground.

Returns:

An RLEMask object representing the mask.

Raises:

ValueError – If neither path nor data is provided, or both are provided, or if the PNG is not 8-bit grayscale.

Return type:

RLEMask

Note

Only 8-bit grayscale PNGs are supported. For other formats, use PIL/Pillow/OpenCV to load the image and convert with RLEMask.from_array().