ImageCompression
Re-encode the image as lossy JPEG or WebP and decode it, adding the blocking, ringing, and color-smearing artifacts of photos saved at reduced quality.
Use it to make a model robust to compression it meets in real data, sampling a range of qualities during training, or to measure how accuracy falls as quality drops by fixing one quality at a time during evaluation. Encoding and decoding happen in memory; nothing is written to disk.
quality_rangeInclusive range of integer qualities in [1, 100]. One quality is sampled uniformly each time the transform is applied. Lower values discard more detail; 1 is the strongest compression and 100 the weakest. Set both endpoints equal for a fixed quality. Default: (99, 100).
compression_typeCodec used for the round trip. Default: "jpeg".
pProbability of applying the transform. Default: 0.5.
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.default_rng(137).integers(0, 256, (100, 100, 3), dtype=np.uint8)
>>> transform = A.ImageCompression(quality_range=(30, 70), compression_type="webp", p=1.0)
>>> compressed = transform(image=image)["image"]
>>> compressed.shape == image.shape
True- Every channel is image data and receives artifacts. The first three channels are compressed together as a color image. Each further channel is compressed separately as a grayscale plane. One channel is compressed as grayscale, and two channels are padded with a zero third channel for encoding.
- A fourth channel is an ordinary channel, not an alpha mask. Pass masks and labels through
maskormasks, which this transform does not change. - Floating-point images pass through uint8 during encoding and are restored to the input dtype, so values are quantized to 256 levels.
Downscale: Lose fine detail by shrinking and enlarging the image instead of re-encoding it. Posterize: Reduce intensity levels per channel to produce visible banding.
- JPEG compressionhttps://en.wikipedia.org/wiki/JPEG
- WebP compressionhttps://developers.google.com/speed/webp