decode_gif#
- torchcodec.decoders.decode_gif(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 GIF image into a
[N]CHWtensor.The output shape is
(C, H, W)for a still GIF and(N, C, H, W)for an animated one (N frames).Example
from torchcodec.decoders import decode_gif img = decode_gif("image.gif")
- Parameters:
source (str,
pathlib.Path, bytes, ortorch.Tensor) – The encoded GIF data: a path (strorpathlib.Path), abytesobject, or a 1-D uint8torch.Tensorof 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 aretorch.uint8(default),torch.uint16, and"auto". Since GIF is an 8-bit format,"auto"andtorch.uint8are equivalent.torch.uint16emulates 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:
Examples using
decode_gif: