Rate this Page

decode_webp#

torchcodec.decoders.decode_webp(source: str | Path | bytes | Tensor, *, mode: Literal['UNCHANGED', 'GRAY', 'GRAY_ALPHA', 'RGB', 'RGB_ALPHA'] | ImageReadMode = 'RGB', output_dtype: dtype | Literal['auto'] = torch.uint8) Tensor[source]#

Decode a WebP image into a [N]CHW tensor.

The output shape is (C, H, W) for a still WebP and (N, C, H, W) for an animated one (N frames).

Example

from torchcodec.decoders import decode_webp

img = decode_webp("image.webp")
Parameters:
  • source (str, pathlib.Path, bytes, or torch.Tensor) – The encoded WebP data: a path (str or pathlib.Path), a bytes object, or a 1-D uint8 torch.Tensor of the raw encoded bytes.

  • mode (str or ImageReadMode, optional) – Desired color mode of the output image. Can be one of "UNCHANGED", "GRAY", "GRAY_ALPHA", "RGB", or "RGB_ALPHA". Default is "RGB".

  • output_dtype (torch.dtype or "auto", optional) – desired dtype of the output image tensor. Accepted values are torch.uint8 (default), torch.uint16, and "auto". Since WebP is an 8-bit format, "auto" and torch.uint8 are equivalent. torch.uint16 emulates a 16-bit output by scaling the 8-bit values to the full 16-bit range (0-255 -> 0-65535).

Returns:

The decoded image, of shape (C, H, W) (still) or (N, C, H, W) (animated).

Return type:

torch.Tensor

Examples using decode_webp:

Decoding images

Decoding images