• AlbumentationsAlbumentations
All TransformsGet LicenseDocumentationNews & Insights
Report IssueJoin Discord...

Colorize

Targets:
image
Image Types:uint8, float32

Map a single-channel grayscale image to a 2- or 3-color RGB gradient with per-call sampled anchor colors (Pillow ImageOps.colorize style).

Intensity acts as a coordinate along a sampled color ramp:

  • 0 maps to a sample from black_range
  • 255 (or 1.0 for float32) maps to a sample from white_range
  • if mid_range is set, intensity sampled from mid_value_range maps to a sample from mid_range and the ramp becomes piecewise linear

Each anchor range is given as (low_rgb, high_rgb) and sampled per-channel uniformly on every call. Pass identical low/high tuples to fix a color (e.g. black_range=((0, 0, 255), (0, 0, 255))). Anchors are always specified in 0-255 RGB; for float32 inputs they are rescaled to [0, 1] internally.

Arguments
black_range
tuple[tuple[int, int, int], tuple[int, int, int]]
[[0,0,0],[0,0,0]]

Inclusive per-channel range from which the dark anchor is sampled. Default: ((0, 0, 0), (0, 0, 0)).

white_range
tuple[tuple[int, int, int], tuple[int, int, int]]
[[255,255,255],[255,255,255]]

Inclusive per-channel range from which the bright anchor is sampled. Default: ((255, 255, 255), (255, 255, 255)).

mid_range
tuple[tuple[int, int, int], tuple[int, int, int]] | None

Optional inclusive range from which the midpoint anchor is sampled. None disables 3-color mode. Default: None.

mid_value_range
tuple[int, int]
[127,127]

Inclusive intensity range (each in 1-254) from which the midpoint position is sampled. Ignored when mid is None. Default: (127, 127).

p
float
0.5

Probability of applying the transform. Default: 0.5.

Examples
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 1), dtype=np.uint8)
>>>
>>> # Fixed blue -> yellow ramp (low == high)
>>> fixed = A.Compose([A.Colorize(
...     black_range=((0, 0, 255), (0, 0, 255)),
...     white_range=((255, 255, 0), (255, 255, 0)),
...     p=1.0,
... )])
>>> assert fixed(image=image)["image"].shape == (100, 100, 3)
>>>
>>> # Random thermal-ish ramp with random midpoint position
>>> random_thermal = A.Compose([A.Colorize(
...     black_range=((0, 0, 64), (32, 0, 192)),
...     mid_range=((96, 0, 96), (160, 64, 160)),
...     white_range=((220, 160, 0), (255, 220, 32)),
...     mid_value_range=(96, 160),
...     p=1.0,
... )])
>>> assert random_thermal(image=image)["image"].shape == (100, 100, 3)
Notes
  • Input must be single-channel; multi-channel input is a no-op with a warning.
  • Interpolation is linear in RGB space.
  • For uint8 inputs the per-call mapping is a (256, 3) LUT applied via cv2.LUT; for float32 inputs np.interp is used per channel.