LCMa
LCMa

Reputation: 445

get grayscale image from jpeg

I have this jpeg image

enter image description here

I want to convert it to a grayscale image. But when applaying

img = cv2.imread(filename)
grey_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

I get this image, which is not a grayscale image ...

enter image description here

Why I didn't get a grayscale image ? How can I obtained it ?

Upvotes: 1

Views: 327

Answers (1)

sacuL
sacuL

Reputation: 51335

Looks like you're probably using matplotlib to display your image. When you do that, make sure you use a grey colormap. The default is Viridis (see docs), which is the blue-yellow colormap you're seeing

plt.imshow(grey_img, cmap = "gray")
plt.show()

As noted in the imshow docs, pseudocolor will be added according to the colormap when you pass a 2D array:

The input may either be actual RGB(A) data, or 2D scalar data, which will be rendered as a pseudocolor image. For displaying a grayscale image set up the colormapping using the parameters cmap='gray', vmin=0, vmax=255.

Upvotes: 3

Related Questions