How to Create a Black Image and a White Image using OpenCV Python

share link

by vigneshchennai74 dot icon Updated: Feb 7, 2023

technology logo
technology logo

Solution Kit Solution Kit  

Converting a black image to white means changing the color of the image from black to white. This can be done using increasing and decreasing the pixels of the color. The specific steps will vary depending on the software being used, but the basic idea is to increase the brightness until the black pixels become white.


OpenCV is widely used in the field of computer vision for tasks such as object recognition, face detection, and image and video analysis. It has a large community of developers and users and is continuously updated and improved. cv2.threshold is a function in OpenCV (Open Source Computer Vision Library) that applies a threshold to an image. Thresholding is a process of converting an image into a binary image by dividing the image into two regions, foreground, and background. The foreground pixels have a certain intensity level and all other pixels are set to the background. The threshold is the value that separates the foreground and background pixels.


  • Reads an image using cv2.imread and stores it in the img variable.
  • Converts the image to grayscale using cv2.cvtColor and stores it in the gray variable.
  • Applies a binary threshold to the grayscale image using cv2.threshold, which means that all pixels with a value greater than or equal to 240 are set to 255 (white) and all other pixels are set to 0 (black). The thresholded image is stored in the thresh variable.
  • Defines two arrays white_px and black_px to represent the white and black pixels, respectively.
  • Loops through each row and column of the thresh image and if the pixel value is equal to white, it sets the corresponding pixel in the img_array to black.
  • Creates a structuring element using cv2.getStructuringElement with an elliptical shape and size 5x5.
  • Applies morphological erosion to the img_array using the created structuring element and 1 iteration. The result is stored in the erosion variable.
  • Displays the erosion image in a window using cv2.imshow and waits for a key press using cv2.waitKey. The window is closed using cv2.destroyAllWindows..


Here is an example of how you can convert white pixels to Black using openCV.

Preview of the output that you will get on running this code from your IDE

CODE

In this solution we use the Imread function of the OpenCV.


  1. Copy the code using the "Copy" button above, and paste it in a Python file in your IDE.
  2. Import open Cv library and Numpy library.
  3. Modify the name, location of the image in the code.
  4. Run the file to get Output


I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


i found this code snippet by searching for "Convert White Pixels to Black in OpenCV python" in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions


  1. The solution is created and executed in python version 3.7.15 .
  2. The solution is tested on OpenCV 4.6.0
  3. The solution is tested on numpy 1.21.6


Using this solution, we are going to convert white Pixels to Black using the OpenCV library in Python with simple steps. This process also facilities an easy-to-use, hassle-free method to create a hands-on working version of code which would help us change the pixels in Python

Dependent Library

opencvby opencv

C++ doticonstar image 69456 doticonVersion:4.7.0doticon
License: Permissive (Apache-2.0)

Open Source Computer Vision Library

Support
    Quality
      Security
        License
          Reuse

            opencvby opencv

            C++ doticon star image 69456 doticonVersion:4.7.0doticon License: Permissive (Apache-2.0)

            Open Source Computer Vision Library
            Support
              Quality
                Security
                  License
                    Reuse

                      numpyby numpy

                      Python doticonstar image 23755 doticonVersion:v1.25.0rc1doticon
                      License: Permissive (BSD-3-Clause)

                      The fundamental package for scientific computing with Python.

                      Support
                        Quality
                          Security
                            License
                              Reuse

                                numpyby numpy

                                Python doticon star image 23755 doticonVersion:v1.25.0rc1doticon License: Permissive (BSD-3-Clause)

                                The fundamental package for scientific computing with Python.
                                Support
                                  Quality
                                    Security
                                      License
                                        Reuse

                                          If you do not have OpenCV and numpy that is required to run this code, you can install it by clicking on the above link and copying the pip Install command from the OpenCV page in kandi.

                                          You can search for any dependent library on kandi like OpenCV and numpy

                                          Support

                                          1. For any support on kandi solution kits, please use the chat
                                          2. For further learning resources, visit the Open Weaver Community learning page.


                                          See similar Kits and Libraries