I extract pages images from a PDF file in jpeg format and I need to determine if each image is much more grayscale, color ou black and white (with a tolerance factor).
I have found some ways to work with color detection with PIL ( here and here ) but I can't figure out how to answer this simple (visual) question : is it much more black and white, color or grayscale image ?
I prefer working with Python and PIL for this part but I could use too OpenCV if someone has a clue (or solution).
It is important to distinguish between RGB images and grayscale images. An RGB image has three color channels: Red channel, Green channel and Blue channel. However, a grayscale image has just one channel.
These pixel values denote the intensity of the pixels. For a grayscale or b&w image, we have pixel values ranging from 0 to 255. The smaller numbers closer to zero represent the darker shade while the larger numbers closer to 255 represent the lighter or the white shade.
We use this simple function to determine the color-factor of an image.
# Iterate over all Pixels in the image (width * height times) and do this for every pixel:
{
int rg = Math.abs(r - g);
int rb = Math.abs(r - b);
int gb = Math.abs(g - b);
diff += rg + rb + gb;
}
return diff / (height * width) / (255f * 3f);
As gray values have r-g = 0 and r-b = 0 and g-b = 0 diff will be near 0 for grayscale images and > 0 for colored images.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With