Python | Grayscaling of Images using OpenCV Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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.Importance of grayscaling Dimension reduction: For example, In RGB images there are three color channels and three dimensions while grayscale images are single-dimensional.Reduces model complexity: Consider training neural articles on RGB images of 10x10x3 pixels. The input layer will have 300 input nodes. On the other hand, the same neural network will need only 100 input nodes for grayscale images.For other algorithms to work: Many algorithms are customized to work only on grayscale images e.g. Canny edge detection function pre-implemented in the OpenCV library works on Grayscale images only.Let's learn the different image processing methods to convert a colored image into a grayscale image.Method 1: Using the cv2.cvtColor() functionImport the OpenCV and read the original image using imread() than convert to grayscale using cv2.cvtcolor() function. destroyAllWindows() function allows users to destroy or close all windows at any time after exiting the script. Python # import opencv import cv2 # Load the input image image = cv2.imread('C:\\Documents\\full_path\\tomatoes.jpg') cv2.imshow('Original', image) cv2.waitKey(0) # Use the cvtColor() function to grayscale the image gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow('Grayscale', gray_image) cv2.waitKey(0) # Window shown waits for any key pressing event cv2.destroyAllWindows() Input image: Output Image: Method 2: Using the cv2.imread() function with flag=zeroImport the OpenCV and read the original image using imread() than convert to grayscale using cv2.cvtcolor() function. Python # Import opencv import cv2 # Use the second argument or (flag value) zero # that specifies the image is to be read in grayscale mode img = cv2.imread('C:\\Documents\\full_path\\tomatoes.jpg', 0) cv2.imshow('Grayscale Image', img) cv2.waitKey(0) # Window shown waits for any key pressing event cv2.destroyAllWindows() Output Image: Method 3: Using the pixel manipulation (Average method) Python # Import opencv import cv2 # Load the input image img = cv2.imread('C:\\Documents\\full_path\\tomatoes.jpg') # Obtain the dimensions of the image array # using the shape method (row, col) = img.shape[0:2] # Take the average of pixel values of the BGR Channels # to convert the colored image to grayscale image for i in range(row): for j in range(col): # Find the average of the BGR pixel values img[i, j] = sum(img[i, j]) * 0.33 cv2.imshow('Grayscale Image', img) cv2.waitKey(0) # Window shown waits for any key pressing event cv2.destroyAllWindows() Output Image: Hope you have understood the above-discussed image processing techniques to convert a colored image into a grayscale image in Python! Comment More infoAdvertise with us S Sourabh_Sinha Follow Improve Article Tags : Python AI-ML-DS Image-Processing OpenCV Python-OpenCV +1 More Practice Tags : python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 6 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 8 min read Recursion in Python 6 min read Python Lambda Functions 6 min read Python Data StructuresPython String 6 min read Python Lists 6 min read Python Tuples 6 min read Dictionaries in Python 7 min read Python Sets 10 min read Python Arrays 9 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 11 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 10 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like