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 或 float 张量。注意
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[图像通道数, 图像高度, 图像宽度])
使用
decode_image
的示例