Shortcuts

decode_jpeg

torchvision.io.decode_jpeg(input: Union[Tensor, List[Tensor]], mode: ImageReadMode = ImageReadMode.UNCHANGED, device: Union[str, device] = 'cpu', apply_exif_orientation: bool = False) Union[Tensor, List[Tensor]][source]

Decode JPEG image(s) into 3D RGB or grayscale Tensor(s), on CPU or CUDA.

The values of the output tensor are uint8 between 0 and 255.

Note

When using a CUDA device, passing a list of tensors is more efficient than repeated individual calls to decode_jpeg. When using CPU the performance is equivalent. The CUDA version of this function has explicitly been designed with thread-safety in mind. This function does not return partial results in case of an error.

Parameters:
  • input (Tensor[1] or list[Tensor[1]]) – a (list of) one dimensional uint8 tensor(s) containing the raw bytes of the JPEG image. The tensor(s) must be on CPU, regardless of the device parameter.

  • mode (str or ImageReadMode) – The mode to convert the image to, e.g. “RGB”. Default is “UNCHANGED”. See ImageReadMode for available modes.

  • device (str or torch.device) –

    The device on which the decoded image will be stored. If a cuda device is specified, the image will be decoded with nvjpeg. This is only supported for CUDA version >= 10.1

    Warning

    The device parameter is in Beta stage, and backward compatibility is not guaranteed.

    Warning

    There is a memory leak in the nvjpeg library for CUDA versions < 11.6. Make sure to rely on CUDA 11.6 or above before using device="cuda".

  • apply_exif_orientation (bool) – apply EXIF orientation transformation to the output tensor. Default: False. Only implemented for JPEG format on CPU.

Returns:

The values of the output tensor(s) are uint8 between 0 and 255. output.device will be set to the specified device

Return type:

output (Tensor[image_channels, image_height, image_width] or list[Tensor[image_channels, image_height, image_width]])

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources