Give grayscale to an Image with OpenCV

We read an image from the local Windows system in the previous lesson. Let us give it a grayscale now using the same imread() function. Just set the last parameter with the value 0 to get a grayscale image. You can also use cv2.IMREAD_GRAYSCALE to implement the same:].

Here is the code:

The following is the output:

Give grayscale to an Image with OpenCV
Explanation:

This Python script utilizes the OpenCV library to load, display, and convert an image to grayscale. The code starts by importing the OpenCV library:

  • It then reads an image from the specified file path and loads it into a variable named image.
  • By passing 0 as the second argument to the cv2.imread() function, the image is loaded in grayscale mode.
  • The script then displays the grayscale image in a window titled “My YouTube Channel’s cake” using the cv2.imshow() function.
  • To keep the image window open, the script includes a cv2.waitKey(0) function, which waits indefinitely for a key press.
  • Once a key is pressed, the script closes all OpenCV windows using the cv2.destroyAllWindows() function.
    This process allows users to view the grayscale version of the loaded image and close the display window at their convenience.

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Display an Image with OpenCV
Resize an image with OpenCV
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading