I am promoting my comment to an answer:

The easy way is:

You could draw in the original 'frame' itself instead of using gray image.

The hard way (method you were trying to implement):

backtorgb = cv2.cvtColor(gray,cv2.COLOR_GRAY2RGB)

is the correct syntax.

Answer from Anoop K. Prabhu on Stack Overflow
Top answer
1 of 5
216

I am promoting my comment to an answer:

The easy way is:

You could draw in the original 'frame' itself instead of using gray image.

The hard way (method you were trying to implement):

backtorgb = cv2.cvtColor(gray,cv2.COLOR_GRAY2RGB)

is the correct syntax.

2 of 5
18

Alternatively, cv2.merge() can be used to turn a single channel binary mask layer into a three channel color image by merging the same layer together as the blue, green, and red layers of the new image. We pass in a list of the three color channel layers - all the same in this case - and the function returns a single image with those color channels. This effectively transforms a grayscale image of shape (height, width, 1) into (height, width, 3)

To address your problem

I did some thresholding on an image and want to label the contours in green, but they aren't showing up in green because my image is in black and white.

This is because you're trying to display three channels on a single channel image. To fix this, you can simply merge the three single channels

image = cv2.imread('image.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_three = cv2.merge([gray,gray,gray])

Example

We create a color image with dimensions (200,200,3)

image = (np.random.standard_normal([200,200,3]) * 255).astype(np.uint8)

Next we convert it to grayscale and create another image using cv2.merge() with three gray channels

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_three = cv2.merge([gray,gray,gray])

We now draw a filled contour onto the single channel grayscale image (left) with shape (200,200,1) and the three channel grayscale image with shape (200,200,3) (right). The left image showcases the problem you're experiencing since you're trying to display three channels on a single channel image. After merging the grayscale image into three channels, we can now apply color onto the image

contour = np.array([[10,10], [190, 10], [190, 80], [10, 80]])
cv2.fillPoly(gray, [contour], [36,255,12])
cv2.fillPoly(gray_three, [contour], [36,255,12])

Full code

import cv2
import numpy as np

# Create random color image
image = (np.random.standard_normal([200,200,3]) * 255).astype(np.uint8)

# Convert to grayscale (1 channel)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Merge channels to create color image (3 channels)
gray_three = cv2.merge([gray,gray,gray])

# Fill a contour on both the single channel and three channel image
contour = np.array([[10,10], [190, 10], [190, 80], [10, 80]])
cv2.fillPoly(gray, [contour], [36,255,12])
cv2.fillPoly(gray_three, [contour], [36,255,12])

cv2.imshow('image', image)
cv2.imshow('gray', gray)
cv2.imshow('gray_three', gray_three)
cv2.waitKey()
๐ŸŒ
Medium
ninza7.medium.com โ€บ image-processing-convert-rgb-image-into-grayscale-with-few-lines-of-python-code-b2d2745ccaea
Image Processing: Convert RGB Image into grayscale with few lines of Python Code | by Rohit Kumar Thakur | Medium
April 4, 2023 - Convert your image from RGB to Lab Color Space. ... Set the color values of the second and third channels to zeros. ... Get the grayscale image by converting the image back to RGB from Lab Color Space.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-grayscaling-of-images-using-opencv
Python | Grayscaling of Images using OpenCV - GeeksforGeeks
September 23, 2025 - This method converts an image to grayscale by averaging the contributions of color channels (RGB).
๐ŸŒ
YouTube
youtube.com โ€บ watch
How can I convert an RGB image into grayscale in Python - YouTube
๐Ÿ“Œ How can I convert an RGB image into grayscale in Python. In this video we will convert out image in grayscale image in python programming language.โž–โž–โž–โž–โž–โž–โž–...
Published ย  September 4, 2022
๐ŸŒ
scikit-image
scikit-image.org โ€บ docs โ€บ stable โ€บ auto_examples โ€บ color_exposure โ€บ plot_rgb_to_gray.html
RGB to grayscale โ€” skimage 0.26.0 documentation
import matplotlib.pyplot as plt from skimage import data from skimage.color import rgb2gray original = data.astronaut() grayscale = rgb2gray(original) fig, axes = plt.subplots(1, 2, figsize=(8, 4)) ax = axes.ravel() ax[0].imshow(original) ax[0].set_title("Original") ax[1].imshow(grayscale, cmap=plt.cm.gray) ax[1].set_title("Grayscale") fig.tight_layout() plt.show()
๐ŸŒ
scikit-image
scikit-image.org โ€บ docs โ€บ dev โ€บ auto_examples โ€บ color_exposure โ€บ plot_rgb_to_gray.html
RGB to grayscale โ€” skimage 0.26.1rc0.dev0 documentation
import matplotlib.pyplot as plt from skimage import data from skimage.color import rgb2gray original = data.astronaut() grayscale = rgb2gray(original) fig, axes = plt.subplots(1, 2, figsize=(8, 4)) ax = axes.ravel() ax[0].imshow(original) ax[0].set_title("Original") ax[1].imshow(grayscale, cmap=plt.cm.gray) ax[1].set_title("Grayscale") fig.tight_layout() plt.show()
๐ŸŒ
GitHub
github.com โ€บ deenaariff โ€บ GrayScale
GitHub - deenaariff/GrayScale: A small python script to convert images to greyscales by averaging RGB values per pixel. ยท GitHub
A small python script to convert images to greyscales by averaging RGB values per pixel. - deenaariff/GrayScale
Starred by 11 users
Forked by 4 users
Languages ย  Python
Find elsewhere
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-convert-an-image-to-grayscale-using-numpy-arrays-a-comprehensive-guide
How to Convert an Image to Grayscale Using NumPy Arrays: A Guide | Saturn Cloud Blog
January 14, 2024 - Weโ€™ll use the formula 0.2989 * R + 0.5870 * G + 0.1140 * B, which corresponds to the way humans perceive color intensity. def rgb2gray(rgb): return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140]) gray = rgb2gray(img)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ computer vision โ€บ how-to-convert-a-grayscale-image-to-rgb-in-opencv
How to convert a grayscale image to RGB in OpenCV - GeeksforGeeks
July 23, 2025 - In RGB images, each pixel is defined by three separate values, one for each color channel. Each channel typically uses 8 bits, allowing for 256 intensity levels per channel. The combination of these channels produces the final color of the pixel. For instance, a pixel with values (255, 0, 0) will be bright red, while (0, 255, 0) will be bright green, and (0, 0, 255) will be bright blue. Converting grayscale images to RGB can be essential for several reasons:
๐ŸŒ
Brandonrohrer
brandonrohrer.com โ€บ convert_rgb_to_grayscale
How to Convert an RGB Image to Grayscale - Brandon Rohrer
import lodgepole.image_tools as lit gray_img = lit.rgb2gray(color_img) If after reading this far you insist on straight up averaging the three channels together, I will judge you. Now go make beautiful grayscale images!
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-can-scikit-learn-be-used-to-convert-an-image-from-rgb-to-grayscale-in-python
How can scikit-learn be used to convert an image from RGB to grayscale in Python?
from skimage import io import matplotlib.pyplot as plt from skimage import data from skimage.color import rgb2gray from skimage import data path = "path to puppy_1.JPG" orig_img = io.imread(path) grayscale_img = rgb2gray(orig_img) fig, axes = plt.subplots(1, 2, figsize=(8, 4)) ax = axes.ravel() ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-convert-rgb-to-grayscale-in-python-using-scikit-image
How to convert RGB to grayscale in Python using scikit-image
To convert an RGB image to a grayscale image using scikit-image (skimage) in Python, we can use the rgb2gray() function from the skimage.color module as shown in the syntax below:
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ convert an rgb image into grayscale using matplotlib
Convert an RGB image into grayscale using Matplotlib - AskPython
February 16, 2023 - Using Scikit Learn to Grayscale Images. This tutorial covers the basic image manipulation techniques involving matplotlib, NumPy and scikit learn. Converting a colored image into black and white can be useful on many occasions to reduce the size of a program and remove unnecessary pixel data. You can learn more about image processing using python here further.
๐ŸŒ
Medium
medium.com โ€บ @nutanbhogendrasharma โ€บ convert-a-color-image-to-gray-scale-in-python-7fe952934c0a
Convert a Color Image to Gray Scale in Python | by Nutan | Medium
July 27, 2023 - Convert a Color Image to Gray Scale in Python In this blog, we will convert color images into grayscale using the Pillow and CV2 packages. Installation We will use opencv-python and the pillow module โ€ฆ
๐ŸŒ
Cloudinary
cloudinary.com โ€บ home โ€บ a guide to converting images to grayscale with python introduction
A Guide to Converting Images to Grayscale with Python Introduction | Cloudinary
April 21, 2024 - Pillow is a fork of PIL (Python Imaging Library) with a robust tool-set for image processing in Python. It provides a wide range of functions for loading, manipulating, and saving images in various formats. Hereโ€™s an example of how to convert an image to grayscale using Pillow:
๐ŸŒ
scikit-image
scikit-image.org โ€บ docs โ€บ 0.25.x โ€บ auto_examples โ€บ color_exposure โ€บ plot_rgb_to_gray.html
RGB to grayscale โ€” skimage 0.25.2 documentation
import matplotlib.pyplot as plt from skimage import data from skimage.color import rgb2gray original = data.astronaut() grayscale = rgb2gray(original) fig, axes = plt.subplots(1, 2, figsize=(8, 4)) ax = axes.ravel() ax[0].imshow(original) ax[0].set_title("Original") ax[1].imshow(grayscale, cmap=plt.cm.gray) ax[1].set_title("Grayscale") fig.tight_layout() plt.show()