decode_image¶
- torchvision.io.decode_image(input: Union[Tensor, str], mode: ImageReadMode = ImageReadMode.UNCHANGED, apply_exif_orientation: bool = False) Tensor [source]¶
将图像从路径或原始编码字节解码为 uint8 张量。
目前支持的图像格式为 jpeg、png、gif 和 webp。
在大多数情况下,输出张量的值为 uint8,范围为 [0, 255]。
如果图像是 16 位 png,则输出张量为 uint16,范围为 [0, 65535](torchvision
0.21
起支持)。由于 pytorch 中对 uint16 的支持有限,我们建议在此函数之后调用torchvision.transforms.v2.functional.to_dtype()
并使用scale=True
,以将解码后的图像转换为 uint8 或浮点张量。注意
decode_image()
尚不适用于 AVIF 或 HEIC 图像。对于这些格式,请直接调用decode_avif()
或decode_heic()
。- 参数:
input (Tensor 或 str 或
pathlib.Path
) – 要解码的图像。如果传递张量,则它必须是包含图像原始字节的一维 uint8 张量。否则,这必须是图像文件的路径。mode (str 或 ImageReadMode) – 将图像转换为的模式,例如“RGB”。默认为“UNCHANGED”。有关可用模式,请参阅
ImageReadMode
。apply_exif_orientation (bool) – 将 EXIF 方向转换应用于输出张量。仅适用于 JPEG 和 PNG 图像。默认值:False。
- 返回:
output (Tensor[image_channels, image_height, image_width])
使用
decode_image
的示例