ImageCompression
Targets:
image
volume
Image Types:uint8, float32
Decrease image quality by applying JPEG or WebP compression.
This transform simulates the effect of saving an image with lower quality settings, which can introduce compression artifacts. It's useful for data augmentation and for testing model robustness against varying image qualities.
Arguments
quality_rangetuple[int, int]
[99,100]
Range for the compression quality. The values should be in [1, 100] range, where:
- 1 is the lowest quality (maximum compression)
- 100 is the highest quality (minimum compression) Default: (99, 100)
compression_typejpeg | webp
jpeg
Type of compression to apply.
- "jpeg": JPEG compression
- "webp": WebP compression Default: "jpeg"
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)
>>> transform = A.ImageCompression(quality_range=(50, 90), compression_type=0, p=1.0)
>>> result = transform(image=image)
>>> compressed_image = result["image"]Notes
- This transform expects images with 1, 3, or 4 channels.
- For JPEG compression, alpha channels (4th channel) will be ignored.
- WebP compression supports transparency (4 channels).
- The actual file is not saved to disk; the compression is simulated in memory.
- Lower quality values result in smaller file sizes but may introduce visible artifacts.
- This transform can be useful for:
- Data augmentation to improve model robustness
- Testing how models perform on images of varying quality
- Simulating images transmitted over low-bandwidth connections
References
- JPEG compressionhttps://en.wikipedia.org/wiki/JPEG
- WebP compressionhttps://developers.google.com/speed/webp