Shortcuts

to_grayscale

torchvision.transforms.functional.to_grayscale(img, num_output_channels=1)[source]

将任何模式(RGB、HSV、LAB等)的PIL图像转换为灰度版本的图像。 此转换不支持torch张量。

Parameters:
  • img (PIL Image) – 要转换为灰度的PIL图像。

  • num_output_channels (int) – 输出图像的通道数。值可以是1或3。默认值为1。

Returns:

图像的灰度版本。

  • 如果 num_output_channels = 1 : 返回的图像是单通道

  • 如果 num_output_channels = 3 : 返回的图像是3通道,且 r = g = b

Return type:

PIL 图像

使用 to_grayscale 的示例:

变换的图示

Illustration of transforms