How to convert an image to grayscale in PyTorch Last Updated : 07 Nov, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we are going to see how to convert an image to grayscale in PyTorch. torchvision.transforms.grayscale method Grayscaling is the process of converting an image from other color spaces e.g. RGB, CMYK, HSV, etc. to shades of gray. It varies between complete black and complete white. torchvision.transforms.grayscale() method is used to convert an image to grayscale. If the input image is torch Tensor then it is expected to have [3, H, W] shape, H, W is height and width respectively. The below syntax is used to convert an image to grayscale. Package RequirementPytorch is an open-source deep learning framework available with a Python and C++ interface. Pytorch resides inside the torch module. In PyTorch, the data that has to be processed is input in the form of a tensor. whereas Torchvision is a library that goes hand in hand with PyTorch.pip install torchvision pip install torchPython Pillow is built on top of PIL (Python Image Library) and is considered the fork for the same as PIL.pip install PillowImage used for demonstration: Example The following program is to understand how to convert images to grayscale. Syntax: torchvision.transforms.Grayscale() Parameter: num_output_channels (int) – (1 or 3) number of channels desired for output image Return: This method return an grayscale image. Python3 # import required libraries import torch import torchvision.transforms as transforms from PIL import Image # Read the image picture = Image.open('geekslogo.png') # Define transform transform = transforms.Grayscale() # Convert the image to grayscale image = transform(picture) # Display image.show() Output: Comment More infoAdvertise with us Next Article How to convert an image to grayscale in PyTorch M mukulsomukesh Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads How to adjust the contrast of an image in PyTorch In this article, we are going to see how to adjust the contrast of an image in PyTorch using Python. We can adjust the contrast of an image by using the adjust_contrast() method. adjust_contrast() method adjust_contrast() method accepts the PIL and tensor images as input. tensor image is a tensor wi 2 min read How to crop an image at center in PyTorch? In this article, we will discuss how to crop an image at the center in PyTorch. CenterCrop() method We can crop an image in PyTorch by using the CenterCrop() method. This method accepts images like PIL Image, Tensor Image, and a batch of Tensor images. The tensor image is a PyTorch tensor with [C, 2 min read How to Convert images to NumPy array? Pictures on a computer are made of tiny dots called pixels. To work with them in Python, we convert them into numbers using a NumPy array is a table of numbers showing each pixelâs color. In this article, weâll learn how to do this using popular Python tools.Loading the images via Pillow LibraryLet 5 min read How to draw bounding boxes on an image in PyTorch? In this article, we are going to see how to draw bounding boxes on an image in PyTorch. draw_bounding_boxes() method The draw_bounding_boxes function helps us to draw bounding boxes on an image. With tensor we provide shapes in [C, H, W], where C represents the number of channels and  H, W represent 2 min read How to Display an Image in Grayscale in Matplotlib? In this article, we are going to depict images using the Matplotlib module in grayscale representation using PIL, i.e. image representation using two colors only i.e. black and white. Syntax: matplotlib.pyplot.imshow(X, cmap=None) Displaying Grayscale image Displaying Grayscale image, store the imag 2 min read Like