Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if image is color, grayscale or black and white with Python/PIL

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).

like image 350
Gepeto Avatar asked Nov 19 '13 10:11

Gepeto


People also ask

How do I know if my image is RGB or grayscale?

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.

How do you know if an image is grayscale?

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.


1 Answers

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.

like image 139
TomB Avatar answered Oct 19 '22 23:10

TomB