decode_image¶
- torchvision.io.decode_image(input: Union[Tensor, str], mode: ImageReadMode = ImageReadMode.UNCHANGED, apply_exif_orientation: bool = False) Tensor[source]¶
[DEPRECATED] Use TorchCodec instead.
Decode an image into a uint8 tensor, from a path or from raw encoded bytes.
Warning
The image decoding and encoding capabilities of TorchVision are deprecated since torchvision 0.29 and will be removed in a future release. They are superseded by the more complete decoders and encoders of TorchCodec (from torchcodec 0.16). Please see this migration guide on how to migrate your code.
Currently supported image formats are jpeg, png, gif and webp.
The values of the output tensor are in uint8 in [0, 255] for most cases.
If the image is a 16-bit png, then the output tensor is uint16 in [0, 65535] (supported from torchvision
0.21). Since uint16 support is limited in pytorch, we recommend callingtorchvision.transforms.v2.functional.to_dtype()withscale=Trueafter this function to convert the decoded image into a uint8 or float tensor.Note
decode_image()doesn’t work yet on AVIF or HEIC images. For these formats, directly calldecode_avif()ordecode_heic().- Parameters:
input (Tensor or str or
pathlib.Path) – The image to decode. If a tensor is passed, it must be one dimensional uint8 tensor containing the raw bytes of the image. Otherwise, this must be a path to the image file.mode (str or ImageReadMode) – The mode to convert the image to, e.g. “RGB”. Default is “UNCHANGED”. See
ImageReadModefor available modes.apply_exif_orientation (bool) – apply EXIF orientation transformation to the output tensor. Only applies to JPEG and PNG images. Default: False.
- Returns:
output (Tensor[image_channels, image_height, image_width])
Examples using
decode_image: