Ask Your Question

Revision history [back]

black and white image detection

I am trying to identify if an image is black and white or a color image using Open CV in python language. i have created a black and white image using MS paint to check the same. even though the image is black white it still has RGB values other than 0 and 255. below is the code i have used and images i have used. the output i am getting is color image. I checked the RGB values they have values other than 0 and 255, i am not able to debug why, can some one help me with this?

image_pixel =img.flatten()

bnw_cnt = sum(np.where((image_pixel == 0) | (image_pixel == 255), 1, 0))

if np.vectorize(bnw_cnt) == np.vectorize(image_pixel):
    print("Black and white image")
else:
    print ("Color image")

image description