pytorch.org/vision/main/generated/torchvision.io.decode_image
The always busy Nicolas Hug was sharing this at work, and I hadn’t realized just how comprehensive the image decoding support had become in TorchVision. Over the last year TorchVision has added a lot of image decoding capabilities and got a better entry API. It should generally be faster than PIL now (with the exception of animated GIFS).
Rather than decoding with PIL:
from PIL import Image
# Load the image
image_path = "chungus.png"
image = Image.open(image_path)
You can use the built in decoders like this:
from torchvision.io import read_file, decode_image
# Load the image as a tensor
image_path = "chungus.png"
image_data = read_file(image_path)
image_tensor = decode_image(image_data)
TorchVision’s transforms support PIL transparently, so you might be using it when not intending! Relatedly, you’ll want to use the v2 transforms if you happen to be using the older versions.
In general this complements the release of TorchCodec which has been improving decoding for video – you now have a really good range of options for decoding media in a PyTorch native way!

