to_grayscale

paddle.vision.transforms. to_grayscale ( img, num_output_channels=1 ) [source]

Converts image to grayscale version of image.

Parameters
  • img (PIL.Image|np.array|paddle.Tensor) – Image to be converted to grayscale.

  • num_output_channels (int, optional) – The number of channels for the output image. Single channel. Default: 1.

Returns

Grayscale version of the image.

if num_output_channels = 1 : returned image is single channel

if num_output_channels = 3 : returned image is 3 channel with r = g = b

Return type

PIL.Image|np.array|paddle.Tensor

Examples

>>> import numpy as np
>>> from PIL import Image
>>> from paddle.vision.transforms import functional as F
>>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8')
>>> fake_img = Image.fromarray(fake_img)
>>> gray_img = F.to_grayscale(fake_img)
>>> print(gray_img.size)
(300, 256)