Posterize
Targets:
image
volume
Image Types:uint8, float32
Reduces the number of bits for each color channel in the image.
This transform applies color posterization, a technique that reduces the number of distinct colors used in an image. It works by lowering the number of bits used to represent each color channel, effectively creating a "poster-like" effect with fewer color gradations.
Arguments
num_bitsint | tuple[int, int] | list[tuple[int, int]]
4
Defines the number of bits to keep for each color channel. Can be specified in several ways:
- Single int: Same number of bits for all channels. Range: [1, 7].
- tuple of two ints: (min_bits, max_bits) to randomly choose from. Range for each: [1, 7].
- list of three ints: Specific number of bits for each channel [r_bits, g_bits, b_bits].
- list of three tuples: Ranges for each channel [(r_min, r_max), (g_min, g_max), (b_min, b_max)]. Default: 4
pfloat
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, 3], dtype=np.uint8)
# Posterize all channels to 3 bits
>>> transform = A.Posterize(num_bits=3, p=1.0)
>>> posterized_image = transform(image=image)["image"]
# Randomly posterize between 2 and 5 bits
>>> transform = A.Posterize(num_bits=(2, 5), p=1.0)
>>> posterized_image = transform(image=image)["image"]
# Different bits for each channel
>>> transform = A.Posterize(num_bits=[3, 5, 2], p=1.0)
>>> posterized_image = transform(image=image)["image"]
# Range of bits for each channel
>>> transform = A.Posterize(num_bits=[(1, 3), (3, 5), (2, 4)], p=1.0)
>>> posterized_image = transform(image=image)["image"]Notes
- The effect becomes more pronounced as the number of bits is reduced.
- This transform can create interesting artistic effects or be used for image compression simulation.
- Posterization is particularly useful for:
- Creating stylized or retro-looking images
- Reducing the color palette for specific artistic effects
- Simulating the look of older or lower-quality digital images
- Data augmentation in scenarios where color depth might vary
References
- Color Quantizationhttps://en.wikipedia.org/wiki/Color_quantization
- Posterizationhttps://en.wikipedia.org/wiki/Posterization