albumentations.augmentations.pixel.compression
Reduce quality by downscale then upscale. scale_min and scale_max control factor. Simulates resolution or compression loss.
Members
- classDownscale
- classImageCompression
Downscaleclass
Downscale(
scale_range: tuple[float, float] = (0.25, 0.25),
interpolation_pair: dict[['downscale', 'upscale'], [0, 6, 1, 2, 3, 4, 5]] = {'upscale': 0, 'downscale': 0},
p: float = 0.5
)Reduce quality by downscale then upscale. scale_min and scale_max control factor. Simulates resolution or compression loss. This transform simulates the effect of a low-resolution image by first downscaling the image to a lower resolution and then upscaling it back to its original size. This process introduces loss of detail and can be used to simulate low-quality images or to test the robustness of models to different image resolutions.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| scale_range | tuple[float, float] | (0.25, 0.25) | Range for the downscaling factor. Should be two float values between 0 and 1, where the first value is less than or equal to the second. The actual downscaling factor will be randomly chosen from this range for each image. Lower values result in more aggressive downscaling. Default: (0.25, 0.25) |
| interpolation_pair | dict[['downscale', 'upscale'], [0, 6, 1, 2, 3, 4, 5]] | {'upscale': 0, 'downscale': 0} | A dictionary specifying the interpolation methods to use for downscaling and upscaling. Should contain two keys: - 'downscale': Interpolation method for downscaling - 'upscale': Interpolation method for upscaling Values should be OpenCV interpolation flags (e.g., cv2.INTER_NEAREST, cv2.INTER_LINEAR, etc.) Default: {'downscale': cv2.INTER_NEAREST, 'upscale': cv2.INTER_NEAREST} |
| p | float | 0.5 | Probability of applying the transform. Should be in the range [0, 1]. Default: 0.5 |
Examples
>>> import albumentations as A
>>> import cv2
>>> transform = A.Downscale(
... scale_range=(0.5, 0.75),
... interpolation_pair={'downscale': cv2.INTER_NEAREST, 'upscale': cv2.INTER_LINEAR},
... p=0.5
... )
>>> transformed = transform(image=image)
>>> downscaled_image = transformed['image']Notes
- The actual downscaling factor is randomly chosen for each image from the range specified in scale_range. - Using different interpolation methods for downscaling and upscaling can produce various effects. For example, using INTER_NEAREST for both can create a pixelated look, while using INTER_LINEAR or INTER_CUBIC can produce smoother results. - This transform can be useful for data augmentation, especially when training models that need to be robust to variations in image quality or resolution.
ImageCompressionclass
ImageCompression(
compression_type: 'jpeg' | 'webp' = jpeg,
quality_range: tuple[int, int] = (99, 100),
p: float = 0.5
)Reduce image quality via JPEG or WebP compression. quality_range and compression_type control strength and format. Simulates real-world compression artifacts. 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.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| compression_type | One of:
| jpeg | Type of compression to apply. - "jpeg": JPEG compression - "webp": WebP compression Default: "jpeg" |
| quality_range | tuple[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) |
| 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, 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
- [{'description': 'JPEG compression', 'source': 'https://en.wikipedia.org/wiki/JPEG'}, {'description': 'WebP compression', 'source': 'https://developers.google.com/speed/webp'}]