unienv_data.transformations.image_compress¶
Classes:
| Name | Description |
|---|---|
ImageCompressTransformation |
|
ImageDecompressTransformation |
|
Functions:
| Name | Description |
|---|---|
get_jpeg_compression_ratio |
|
Attributes:
| Name | Type | Description |
|---|---|---|
JPEG_QUALITY_COMPRESSION_MAP |
|
|
CONSERVATIVE_COMPRESSION_RATIOS |
|
|
CONSERVATIVE_HEADER_BYTES |
|
JPEG_QUALITY_COMPRESSION_MAP
module-attribute
¶
JPEG_QUALITY_COMPRESSION_MAP = {
"quality": np.array([55, 60, 65, 70, 75, 80, 85, 90, 95, 100], dtype=int),
"compression_ratio": np.array(
[43.27, 36.9, 34.24, 31.5, 26.0, 25.06, 19.08, 14.3, 9.88, 5.27], dtype=float
),
"conservative_ratio": 0.6,
}
CONSERVATIVE_COMPRESSION_RATIOS
module-attribute
¶
CONSERVATIVE_COMPRESSION_RATIOS = {'JPEG': get_jpeg_compression_ratio}
ImageCompressTransformation
¶
ImageCompressTransformation(
init_quality: int = 70,
max_size_bytes: Optional[int] = None,
compression_ratio: Optional[float] = None,
mode: Optional[str] = None,
format: str = "JPEG",
last_channel: bool = True,
header_bytes: Optional[int] = None,
)
Bases: DataTransformation
Initialize JPEG compression transformation.
Args:
init_quality: Initial JPEG quality setting (1-100).
max_size_bytes: Maximum allowed size of compressed JPEG in bytes.
compression_ratio: Size budget as H * W * C // compression_ratio + 1 + header_bytes bytes
(defaults to a conservative ratio for format derived from init_quality).
mode: Optional mode for PIL Image (e.g., "RGB", "L"). If None, inferred from input.
format: Image format to use for compression (default "JPEG"). See https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html for options.
last_channel: Whether images are (..., H, W, C) (True) or channel-less (..., H, W) (False).
header_bytes: Fixed allowance added to ratio-based budgets for the per-image format overhead,
which dominates for small images. Defaults to a conservative value for format;
ignored when max_size_bytes is given.
Methods:
| Name | Description |
|---|---|
validate_source_space |
|
get_uint8_dtype |
|
get_target_space_from_source |
|
encode_to_size |
Encode an image (H, W, 3) or (H, W, 1) as JPEG bytes, |
transform |
|
direction_inverse |
|
serialize |
|
deserialize_from |
|
Attributes:
| Name | Type | Description |
|---|---|---|
has_inverse |
|
|
init_quality |
|
|
max_size_bytes |
|
|
compression_ratio |
|
|
mode |
|
|
format |
|
|
last_channel |
|
|
header_bytes |
|
compression_ratio
instance-attribute
¶
compression_ratio = (
compression_ratio
if compression_ratio is not None
else CONSERVATIVE_COMPRESSION_RATIOS.get(format, None)(init_quality)
if max_size_bytes is None
else None
)
header_bytes
instance-attribute
¶
header_bytes = (
header_bytes
if header_bytes is not None
else CONSERVATIVE_HEADER_BYTES.get(format, 0)
)
validate_source_space
¶
validate_source_space(
source_space: Space[Any, BDeviceType, BDtypeType, BRNGType],
) -> None
get_uint8_dtype
staticmethod
¶
get_uint8_dtype(
backend: ComputeBackend[BArrayType, BDeviceType, BDtypeType, BRNGType],
) -> BDtypeType
encode_to_size
¶
Encode an image (H, W, 3) or (H, W, 1) as JPEG bytes, reducing quality until <= max_bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_array
|
np.ndarray, uint8, shape (H, W, 3) RGB or (H, W, 1) grayscale |
required | |
max_bytes
|
maximum allowed size of JPEG file |
required | |
min_quality
|
minimum JPEG quality before giving up (capped at |
20
|
Returns:
| Type | Description |
|---|---|
|
jpeg_bytes (bytes), final_quality (int) |
Raises:
| Type | Description |
|---|---|
ValueError
|
if the image does not fit in |
ImageDecompressTransformation
¶
ImageDecompressTransformation(
target_height: int,
target_width: int,
target_channels: Optional[int] = 3,
mode: Optional[str] = None,
format: Optional[str] = None,
)
Bases: DataTransformation
Initialize JPEG decompression transformation. Args: target_height: Height of the decompressed image. target_width: Width of the decompressed image. mode: Optional mode for PIL Image (e.g., "RGB", "L"). If None, inferred from input. format: Image format to use for decompression (default None, which will try everything). See https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html for options.
Methods:
| Name | Description |
|---|---|
validate_source_space |
|
get_uint8_dtype |
|
get_target_space_from_source |
|
decode_bytes |
Decode JPEG bytes to an image array (H, W, 3). |
transform |
|
direction_inverse |
|
serialize |
|
deserialize_from |
|
Attributes:
| Name | Type | Description |
|---|---|---|
has_inverse |
|
|
target_height |
|
|
target_width |
|
|
target_channels |
|
|
mode |
|
|
format |
|
validate_source_space
staticmethod
¶
validate_source_space(
source_space: Space[Any, BDeviceType, BDtypeType, BRNGType],
) -> None
decode_bytes
¶
Decode JPEG bytes to an image array (H, W, 3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jpeg_bytes
|
bytes
|
bytes of JPEG image |
required |
Returns:
| Name | Type | Description |
|---|---|---|
img_array |
np.ndarray, uint8, shape (H, W, 3) |