I found a way to check with the module PIL ImageStat
(Here).
from PIL import Image, ImageStat
MONOCHROMATIC_MAX_VARIANCE = 0.005
COLOR = 1000
MAYBE_COLOR = 100
def detect_color_image(file):
v = ImageStat.Stat(Image.open(file)).var
is_monochromatic = reduce(lambda x, y: x and y < MONOCHROMATIC_MAX_VARIANCE, v, True)
print file, '-->\t',
if is_monochromatic:
print "Monochromatic image",
else:
if len(v)==3:
maxmin = abs(max(v) - min(v))
if maxmin > COLOR:
print "Color\t\t\t",
elif maxmin > MAYBE_COLOR:
print "Maybe color\t",
else:
print "grayscale\t\t",
print "(",maxmin,")"
elif len(v)==1:
print "Black and white"
else:
print "Don't know..."
The COLOR and MAYBE_COLOR constants are quick options to find the differences between color and grayscale images, apparently not safe.
As an example, some JPEG images that are seen as color but in real are grayscale with some color artifacts due to a scanning process.
In this case, can you read the color of the pixels in the photo? Or have you thought about how to compare the color code?
– Edward Ramos
Hello! It would be interesting for you to read this article on how to ask a good question, so that you have help more quickly and in a targeted way. https://answall.com/help/how-to-ask
– David Alves
If you can read the rgb value of pixels, give to do 3 if’s in the rgb code and know if the color tone is closer to black, and then make the three if’s pro white color tone. And repeat this for each pixel within the image.
– Edward Ramos
Hello, Edward Ramos. Thank you very much for the answer. I think this solution will be valid, I will test. I was looking for some function already ready that did it automatically, but I did not find.
– user157401