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 e
In case of a grayscale image, all channels in a certain pixel are equal (if you only have one channel then you don't have a problem) . So basicly if you list all the pixels with their 3 channels values, you suppose to see that each pixel has all 3 channels equal.
Image.getcolors() returns an unsorted list of (count, pixel) values.
im = Image.open('path_to_image.whatever')
color_count = im.getcolors()
If len(color_count) exceeds 256, this method returns None - meaning that you had more than 256 color-options in your pixel list, hence it is a colored image (in case of grayscale you can only have 256 colors (0,0,0) to (255,255,255)).
So after that you only need :
if color_count:
# your image is grayscale
else:
# your images is colored
Notice this will work only when using the default parameter value of getcolors().
Documentation: https://pillow.readthedocs.io/en/3.0.x/reference/Image.html#PIL.Image.Image.getcolors