Convert RGB to Binary Image in Python (Black and White)

A binary image is the type of image where each pixel is black or white. You can also say the pixels as 0 or 1 value. Here 0 represents black and 1 represents a white pixel.

I often find peoples calling grayscale image as black and white. Now let’s understand what is grayscale and what is a black and white or binary image with the example of my own picture.

Many peoples think the grayscale image as black and white. Many of us will think the below image as black and white:

My GrayScale Image

Example of GrayScale Image

 

But this is a wrong concept. Real binary image or pure black and white image look like you can see below:

My Binary Image (Black and white)

My Binary Image (Black and white)

Each of the pixels of the above binary image is either black or white. Or we cay each pixel value is 0 or 1.

So you have seen the same image as grayscale and binary and got the idea of a binary image.

 

Convert RGB to Binary Image in Python using OpenCV

Now I am going to show you how you can convert RGB to Binary Image or convert a colored image to black and white.

Here we are just going to write a few lines of Python code and it will convert our RGB image into a binary image.

To convert an RGB image into a binary type image, we need OpenCV. So first of all, if we don’t have OpenCV installed, then we can install it via pip:

pip install opencv-python

Now we can continue writing Python code.

At the top, we have to import the OpenCV Python library:

import cv2

After that, read our image as grayscale. Grayscale is a simplified image and it makes the process simple. Below is the code to get grayscale data of the image:

img = cv2.imread('imgs/mypic.jpg',2)

Then set the threshold value for our grayscale image:

ret, bw_img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

Now show the image:

cv2.imshow("Binary Image",bw_img)

In the end, we have to use waitKey and destroyAllWindows method to keep our window always open until we press any key or close our window and also destroy all windows. Below is the Python code that will do that:

cv2.waitKey(0)
cv2.destroyAllWindows()

 

Also, read:

 

The complete and final Python code to convert an RGB or colored image into the binary is given below:

import cv2

img = cv2.imread('imgs/mypic.jpg',2)


ret, bw_img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

cv2.imshow("Binary Image",bw_img)


cv2.waitKey(0)
cv2.destroyAllWindows()

now you can run and test the above code on your system. You have to pass your own image to the imread() method. After you run the code, you will able to see the binary image of the given image which path you have given to the imread() method.

10 responses to “Convert RGB to Binary Image in Python (Black and White)”

  1. shoikot says:

    what is the mean of line 6…”ret”??

    • Faruque Ahamed Mollick says:

      In our code, ret is a boolean regarding whether or not there was a return at all, at the frame is each frame that is returned. If there is no frame available, then you won’t get an error, you will get None instead of the error. So it is preventing your program from getting any error.

  2. santhoshi says:

    The above code is not working

  3. babita iyer says:

    where is your RGB image? you are using grayscale image.

  4. Maryam says:

    if you convert the Image to binary then why you put 255 as max value ??
    Does it should b 1 ?

    • John says:

      if you convert the Image to binary then why you put 255 as max value ??
      Does it should b 1 ?

      Because binary Images pixel values are either 1 or 0. Then why you are putting maximum value as 255 ? Does It is 1 ?? Kindly Clear My confusion??

  5. Maryam says:

    if you convert the Image to binary then why you put 255 as max value ??
    Does it should b 1 ?

    Because binary Images pixel values are either 1 or 0. Then why you are putting maximum value as 255 ? Does It is 1 ?? Kindly Clear My confusion??

  6. jyoti says:

    hello, your code is useful. I want to know if we can use this for a full image dataset

  7. Alejandro says:

    In your code you use 127 as thresh value, Do you know how can I calculate this value for different images?
    I have a set of images but they aren’t very similar and I want to calculate this value for each image.

Leave a Reply

Your email address will not be published. Required fields are marked *