I am reading an image with cv2.imread(), and converting it to grayscale. However why is it not in grayscale when I display it?

Viewed 824

I went to read up the syntax of cv2.imread() method and it says that specifying the flag=0 will load the image in grayscale.

The original image is this:

Original Image

And I executed the following code with the following libs, no errors.

import cv2 import pytesseract import matplotlib import image

img=cv2.imread("C:/Users/HP_Demo/Desktop/cv2/sample02.png",0)

plt.imshow(img)

plt.show()

The result is this: Result image

2 Answers

I do not really know but try using that: cv2.imread("path", cv2.IMREAD_GRAYSCALE).

PS. And it is better to use sys.path.join() instead of raw /

import cv2 

img=cv2.imread("colorful.png",1)
cv2.imshow("",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

colorful

import cv2 

img=cv2.imread("colorful.png",0) # same image changed the 1 to 0
cv2.imshow("",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:

GrayGray

Conclusion

As I said in a comment maybe the image you used is causing the no grayscale.

Related