Stay updated

News & Insights
utils

albumentations.augmentations.pixel.dithering_functional


Functional implementations of dithering algorithms for color depth reduction.

generate_bayer_matrixfunction

generate_bayer_matrix(
    size: int
)

Generate Bayer threshold matrix of given size (cached). Args: size: Size of the matrix (2, 4, 8, or 16). Returns: Bayer matrix normalized to [0, 1] range.

Parameters

NameTypeDefaultDescription
sizeint--

quantize_valuefunction

quantize_value(
    value: float,
    n_levels: int
)

Quantize a single value to n discrete levels. Args: value: Input value in [0, 1] range. n_levels: Number of discrete levels. Returns: Quantized value in [0, 1] range.

Parameters

NameTypeDefaultDescription
valuefloat--
n_levelsint--

quantize_arrayfunction

quantize_array(
    arr: np.ndarray,
    n_levels: int
)

Quantize an array to n discrete levels efficiently using vectorized operations. Args: arr: Input array in [0, 1] range. n_levels: Number of discrete levels. Returns: Quantized array in [0, 1] range.

Parameters

NameTypeDefaultDescription
arrnp.ndarray--
n_levelsint--

random_dither_uint8function

random_dither_uint8(
    img: ImageUInt8,
    n_colors: int,
    noise_range: tuple[float, float],
    random_generator: np.random.Generator
)

Apply random dithering optimized for uint8 images. Args: img: Input uint8 image with shape (H, W, C) in [0, 255] range. n_colors: Number of colors per channel after quantization. noise_range: Range of noise to add (min_noise, max_noise) in [0, 1] range. random_generator: Random number generator for reproducible results. Returns: Dithered uint8 image in [0, 255] range.

Parameters

NameTypeDefaultDescription
imgImageUInt8--
n_colorsint--
noise_rangetuple[float, float]--
random_generatornp.random.Generator--

random_ditherfunction

random_dither(
    img: ImageFloat32,
    n_colors: int,
    noise_range: tuple[float, float],
    random_generator: np.random.Generator
)

Apply random dithering for float32 images. Args: img: Input float32 image with shape (H, W, C) in [0, 1] range. n_colors: Number of colors per channel after quantization. noise_range: Range of noise to add (min_noise, max_noise). random_generator: Random number generator for reproducible results. Returns: Dithered float32 image in [0, 1] range.

Parameters

NameTypeDefaultDescription
imgImageFloat32--
n_colorsint--
noise_rangetuple[float, float]--
random_generatornp.random.Generator--

ordered_dither_uint8function

ordered_dither_uint8(
    img: ImageUInt8,
    n_colors: int,
    matrix_size: int = 4
)

Apply ordered dithering optimized for uint8 images. Args: img: Input uint8 image with shape (H, W, C). n_colors: Number of colors per channel. matrix_size: Size of Bayer matrix (2, 4, 8, or 16). Returns: Dithered uint8 image.

Parameters

NameTypeDefaultDescription
imgImageUInt8--
n_colorsint--
matrix_sizeint4-

ordered_ditherfunction

ordered_dither(
    img: ImageFloat32,
    n_colors: int,
    matrix_size: int = 4
)

Apply ordered dithering using Bayer matrix. Args: img: Input image in [0, 1] range with shape (H, W, C). n_colors: Number of colors per channel. matrix_size: Size of Bayer matrix (2, 4, 8, or 16). Returns: Dithered image in [0, 1] range.

Parameters

NameTypeDefaultDescription
imgImageFloat32--
n_colorsint--
matrix_sizeint4-

error_diffusion_ditherfunction

error_diffusion_dither(
    img: ImageType,
    n_colors: int,
    algorithm: str = floyd_steinberg,
    serpentine: bool = False
)

Apply error diffusion dithering. Args: img: Input image in [0, 1] range with shape (H, W, C). n_colors: Number of colors per channel. algorithm: Error diffusion algorithm name. serpentine: Use serpentine (back-and-forth) scanning. Returns: Dithered image in [0, 1] range.

Parameters

NameTypeDefaultDescription
imgImageType--
n_colorsint--
algorithmstrfloyd_steinberg-
serpentineboolFalse-

apply_ditheringfunction

apply_dithering(
    img: ImageType,
    method: str,
    n_colors: int,
    color_mode: str = per_channel,
    **kwargs: Any
)

Apply dithering to an image. Args: img: Input image in [0, 1] range with shape (H, W, C). method: Dithering method to use. n_colors: Number of colors per channel. color_mode: How to handle colors ("grayscale", "per_channel", "rgb"). **kwargs: Additional parameters for specific methods. Returns: Dithered image in [0, 1] range with shape (H, W, C).

Parameters

NameTypeDefaultDescription
imgImageType--
methodstr--
n_colorsint--
color_modestrper_channel-
**kwargsAny--