Reputation: 91
I want to check if an image is greyscale or color using Python. I know we can read in the image and check easily, however, I am wondering if there is a way to check without reading the full image?
I have around 200 million images (200x200), so reading in each image is not feasible in terms of runtime.
Thanks
Upvotes: 2
Views: 4486
Reputation: 7744
Though I can't say for sure this would be the most efficient, I am certain that it would perform better than most operations.
So consider using ImageStat module.
from PIL import Image, ImageStat
def is_grayscale(path="image.jpg")
im = Image.open(path).convert("RGB")
stat = ImageStat.Stat(im)
if sum(stat.sum)/3 == stat.sum[0]: #check the avg with any element value
return True #if grayscale
else:
return False #else its colour
stat.sum gives us a sum of all pixels in list view = [R, G, B] for example [568283302.0, 565746890.0, 559724236.0]. For grayscale image all elements of list are equal.
Upvotes: 4