kaira.models.image.compressors.WebPCompressor

Inheritance diagram for WebPCompressor
- class kaira.models.image.compressors.WebPCompressor(max_bits_per_image: int | None = None, quality: int | None = None, lossless: bool = False, method: int = 4, exact: bool = False, collect_stats: bool = False, return_bits: bool = True, return_compressed_data: bool = False, *args: Any, **kwargs: Any)[source]
Bases:
BaseImageCompressorWebP image compressor using WebP via PIL/Pillow.
This class provides WebP compression with configurable quality settings and advanced features. WebP is a modern image format developed by Google that provides superior compression efficiency compared to JPEG and PNG while maintaining excellent visual quality. It supports both lossy and lossless compression modes, as well as transparency and animation.
The quality parameter ranges from 1 (worst quality, highest compression) to 100 (best quality, lowest compression). WebP also supports a special lossless mode when lossless=True.
Example
# Fixed quality compression compressor = WebPCompressor(quality=85) compressed_images = compressor(image_batch)
# Bit-constrained compression compressor = WebPCompressor(max_bits_per_image=3500) compressed_images, bits_used = compressor(image_batch)
# Lossless compression compressor = WebPCompressor(lossless=True) compressed_images = compressor(image_batch)
# High-effort compression compressor = WebPCompressor(quality=90, method=6, collect_stats=True, return_bits=True) compressed_images, bits_per_image = compressor(image_batch) stats = compressor.get_stats()
Methods
Initialize the WebP compressor.
Compress a PIL Image to WebP bytes.
Decompress WebP bytes to PIL Image.
Process a batch of images through compression.
Create model instance from configuration.
Create model from Hydra DictConfig.
Create model from Hugging Face PretrainedConfig.
Calculate compression ratio.
Get compression statistics from the last forward pass.
Examples using
kaira.models.image.compressors.WebPCompressor- __init__(max_bits_per_image: int | None = None, quality: int | None = None, lossless: bool = False, method: int = 4, exact: bool = False, collect_stats: bool = False, return_bits: bool = True, return_compressed_data: bool = False, *args: Any, **kwargs: Any)[source]
Initialize the WebP compressor.
- Parameters:
max_bits_per_image – Maximum bits allowed per compressed image. If provided without quality, the compressor will find the highest quality that produces files smaller than this limit.
quality – WebP quality level (1-100, higher = better quality, larger file size). If provided, this exact quality will be used regardless of resulting file size. Ignored when lossless=True.
lossless – Enable lossless compression mode. When True, quality parameter is ignored.
method – Compression method (0-6, higher = slower but potentially better compression). 0 = fastest, 6 = slowest but best compression. Default is 4 for balance.
exact – Preserve RGB values in transparent regions (useful for lossless).
collect_stats – Whether to collect and return compression statistics
return_bits – Whether to return bits per image in forward pass
return_compressed_data – Whether to return the compressed binary data
*args – Variable positional arguments passed to the base class.
**kwargs – Variable keyword arguments passed to the base class.
- compress(image: Image, quality: int | None = None) bytes[source]
Compress a PIL Image to WebP bytes.
This is a convenience method for direct compression without the full forward pass.
- Parameters:
image – PIL Image to compress
quality – WebP quality level (uses instance quality if not provided, ignored if lossless=True)
- Returns:
Compressed WebP data as bytes
- decompress(data: bytes) Image[source]
Decompress WebP bytes to PIL Image.
This is a convenience method for direct decompression.
- Parameters:
data – Compressed WebP data as bytes
- Returns:
Reconstructed PIL Image
- forward(x: Tensor, *args: Any, **kwargs: Any) Tensor | Tuple[Tensor, List[int]] | Tuple[Tensor, List[bytes]] | Tuple[Tensor, List[int], List[bytes]]
Process a batch of images through compression.
- Parameters:
x – Tensor of shape [batch_size, channels, height, width] with values in [0, 1]
*args – Additional positional arguments
**kwargs – Additional keyword arguments
- Returns:
Just the reconstructed image tensor If return_bits=True: Tuple of (tensor, bits per image) If return_compressed_data=True: Tuple of (tensor, compressed binary data) If both are True: Tuple of (tensor, bits per image, compressed binary data)
- Return type:
If no additional returns
- classmethod from_config(config, **kwargs)
Create model instance from configuration.
- Parameters:
config – Configuration object (PretrainedConfig, DictConfig, or dict)
**kwargs – Additional parameters to override config
- Returns:
Model instance
- classmethod from_hydra_config(config: DictConfig, **kwargs)
Create model from Hydra DictConfig.
- Parameters:
config – Hydra configuration
**kwargs – Additional parameters
- Returns:
Model instance
- classmethod from_pretrained_config(config: PretrainedConfig, **kwargs)
Create model from Hugging Face PretrainedConfig.
- Parameters:
config – PretrainedConfig instance
**kwargs – Additional parameters
- Returns:
Model instance