Skip to content

codecs

codecs

Zarr v3 codecs for image compression.

WebPCodec dataclass

WebPCodec(level=99)

Bases: ArrayBytesCodec

WebP image codec for zarr v3 using OpenCV.

Compresses uint8 image array chunks using WebP encoding. Chunks should contain a single image with shape (1, H, W, C) for optimal compression.

Parameters:

Name Type Description Default
level int

WebP quality level (1-100). Higher is better quality but larger files. Default 99 provides near-lossless quality at ~35% less space than JPEG at the same quality.

99
Source code in src/versatil/data/preprocessing/codecs.py
def __init__(self, level: int = 99) -> None:
    object.__setattr__(self, "level", level)

from_dict classmethod

from_dict(data)

Build the codec from a Zarr named-configuration dict.

Source code in src/versatil/data/preprocessing/codecs.py
@classmethod
def from_dict(cls, data: dict[str, JSON]) -> Self:
    """Build the codec from a Zarr named-configuration dict."""
    _, config = parse_named_configuration(
        data, WEBP_CODEC_NAME, require_configuration=False
    )
    config = config or {}
    return cls(**config)

to_dict

to_dict()

Serialize the codec as a Zarr named-configuration dict.

Source code in src/versatil/data/preprocessing/codecs.py
def to_dict(self) -> dict[str, JSON]:
    """Serialize the codec as a Zarr named-configuration dict."""
    return {
        "name": WEBP_CODEC_NAME,
        "configuration": {"level": self.level},
    }

compute_encoded_size

compute_encoded_size(input_byte_length, _chunk_spec)

Encoded size is data-dependent for WebP, so it cannot be precomputed.

Source code in src/versatil/data/preprocessing/codecs.py
def compute_encoded_size(
    self, input_byte_length: int, _chunk_spec: ArraySpec
) -> int:
    """Encoded size is data-dependent for WebP, so it cannot be precomputed."""
    raise NotImplementedError(
        "WebP encoded size is data-dependent and cannot be precomputed."
    )