Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an RGB image contains only one color?

I'm using Python and PIL.

I have images in RGB and I would like to know those who contain only one color (say #FF0000 for example) or a few very close colors (#FF0000 and #FF0001).

I was thinking about using the histogram but it is very hard to figure out something with the 3 color bands, so I'm looking for a more clever algorithm.

Any ideas?

ImageStat module is THE answer! Thanks Aaron. I use ImageStat.var to get the variance and it works perfectly.

Here is my piece of code:

from PIL import Image, ImageStat

MONOCHROMATIC_MAX_VARIANCE = 0.005

def is_monochromatic_image(src):
    v = ImageStat.Stat(Image.open(src)).var
    return reduce(lambda x, y: x and y < MONOCHROMATIC_MAX_VARIANCE, v, True)
like image 511
Grégoire Cachet Avatar asked Mar 24 '09 13:03

Grégoire Cachet


People also ask

What image uses a single color?

In monochrome photography, tones of a single color are used to represent all the different colors within an image.

How do you know if an image is monochrome?

I think a technique that may work for you is to fit a straight line to the point-cloud in the RGB colour-cube of your image and then look at the least squares error and if that is small, the points lie on a line and your image is monochromatic.

How is a single color represented in a digital image?

The pixel is the unit of measure for a digital image. One pixel represents a single color. Colors are represented in hex, RGB, and other identification schemes. Pixels are identified by their location within the coordinate grid.

How many possible colors are there in RGB?

RGB Color Values Each parameter (red, green, and blue) defines the intensity of the color with a value between 0 and 255. This means that there are 256 x 256 x 256 = 16777216 possible colors!


1 Answers

Try the ImageStat module. If the values returned by extrema are the same, you have only a single color in the image.

like image 134
Aaron Digulla Avatar answered Oct 03 '22 02:10

Aaron Digulla