How to check whether a jpeg image is color or gray scale using only Python stdlib

kadina picture kadina · May 14, 2014 · Viewed 47.3k times · Source

I have to write a test case in python to check whether a jpg image is in color or grayscale. Can anyone please let me know if there is any way to do it with out installing extra libraries like opencv?

Answer

superMind picture superMind · Jun 21, 2015

Can be done as follow:

from scipy.misc import imread, imsave, imresize
image = imread(f_name)
if(len(image.shape)<3):
      print 'gray'
elif len(image.shape)==3:
      print 'Color(RGB)'
else:
      print 'others'