Set a Grayscale filter with OpenCV

Convert an image to grayscale to give it a classic black-and-white look. Use the cv2.IMREAD_GRAYSCALE parameter value of the imread() function.

Let us now see the example:

The following is the output:

Set a Grayscale filter with OpenCV
Explanation:

This Python script employs the OpenCV library to load an image, apply a grayscale filter, and display it:

  • It begins by importing the OpenCV library:
  • The script then reads the image file from the specified file path, using the cv2.IMREAD_GRAYSCALE flag in the cv2.imread() function to convert the image into grayscale during the loading process.
  • The grayscale image is assigned to the variable image.
  • The cv2.imshow() function is then used to display the grayscale image in a window titled “Grayscale Filter.”
  • To keep the window open for viewing, the cv2.waitKey(0) function waits indefinitely for a key press.
  • Once a key is pressed, the script closes the display window using the cv2.destroyAllWindows() function.
  • This allows users to view the grayscale-filtered version of the image conveniently and interactively.

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:

Draw a text on an image with OpenCV
Apply Sepia Tone filter 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