Converting Image to Grayscale with Python + OpenCV

Posted: September 23, 2012 in Python
Tags: , , ,

Hey guys, been reading OpenCV for python and thought of posting a tutorial on Programming a Grayscale Image Convertor. I encourage you to google them , there are lots and lots of examples and code snippets. This is on how to a convert any image to gray scale using Python and OpenCV.

A sample input image and output image are shown below (YEah, I am big Iron Man Fan! :B). You can click on image to enlarge:

I have placed an image named “ironman.png” in the current working directory (i.e. I have the original image in the same directory as the  place where I have saved my Python code).

First we import the cv2 module:

import cv2

Then ,read the image to a variable named “image” :

image = cv2.imread('ironman.png')

Now, to convert to gray-scale image and store it to another variable named “gray_image” use the function cv2.cvtColor() with parameters as  the “image” variable and  “cv2.COLOR_BGR2GRAY” :

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

So, now the variable “gray_image” will hold the gray-scale version of the input image.

Now, to write/save the converted gray-scale image to the hard disk, we use the function “cv2.imwrite()” with parameters as “the name of converted image” and the variable “gray_image” to which the converted image was stored:


So, now if you open the directory where you saved your python code, you can see a new image there : gray_image.png! Wow! 😀

Now, to display the original and the gray-scale ,we use function “cv2.imshow()” with parameters as the “window title” and the “image variable” :


Complete Code for GrayScale Image convertor:

# GrayScale Image Convertor

import cv2
image = cv2.imread('ironman.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.waitKey(0)                 # Waits forever for user to press any key
cv2.destroyAllWindows()        # Closes displayed windows

#End of Code

Hope you enjoyed this post, feel free to comment and don’t forget to follow my blog! 😀

  1. seascapesaus says:

    Did I see somewhere on wordpress – “code is poetry”? I can only guess at how poetic this is! Amazing.

  2. tintin says:

    thanks ,I was bothering with the code

  3. Spartacus says:

    Where did you get the cv2 import from?

  4. Shank says:

    It is giving error
    >>> gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    cv2.error: ..\..\..\opencv-2.4.11\modules\imgproc\src\color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

    • ΞXΤЯ3МΞ says:

      @Shank, That usually happens when the image location provided to cv2.imread() is incorrect. Please check the following line (line 5 in the example)in your code:

      image = cv2.imread(‘ironman.png’)

      In my example, I had the ironman.png in the current working directory of the python program itself. So, I just passed inronman.jpg as the parameter.

      If you are selecting an image from an alternate location, make sure you pass the full location as the parameter to the function cv2.imread().

      For example: if the image file is located in C:\Project Files\ironman.jpg , then you code should be like:

      image = cv2.imread(‘C:/Project Files/ironman.png’)

      (You can use forward slash instead of backward slash to understand whitespaces in locations)

      Hope this helps. If you are still having issue with the code, please post your code here and will try to help you out.


  5. Nishad TN says:

    you should type pylab.gray() for correct display of image as gray…

  6. Hilman says:

    What the code “cv2.waitKey(0)” does? It said “Waits forever for user to press any key” but when I press any key, the terminal doesn’t quit. It just stay there. I need to close the terminal and open it again.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s