decode_image¶
- torchvision.io.decode_image(input: Union[Tensor, str], mode: ImageReadMode = ImageReadMode.UNCHANGED, apply_exif_orientation: bool = False) Tensor [源代码]¶
将图像解码为 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 或 float 张量。注意
decode_image()
尚不支持 AVIF 或 HEIC 图像。对于这些格式,请直接调用decode_avif()
或decode_heic()
。- 参数:
input (Tensor 或 str 或
pathlib.Path
) – 要解码的图像。如果传入张量,它必须是包含图像原始字节的一维 uint8 张量。否则,它必须是图像文件的路径。mode (str 或
ImageReadMode
。apply_exif_orientation (bool) – 对输出张量应用 EXIF 方向变换。仅适用于 JPEG 和 PNG 图像。默认为 False。
- 返回:
output (Tensor[图像通道, 图像高度, 图像宽度])
decode_image
的用法示例