Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PIL Detect if an image is completely black or white

Using the Python Imaging Library PIL how can someone detect if an image has all it's pixels black or white?

~Update~

Condition: Not iterate through each pixel!

like image 389
Jimmy Kane Avatar asked Dec 26 '12 13:12

Jimmy Kane


People also ask

How can you tell if a picture is pure black?

By checking for len(clrs) == 1 you can verify if the image contains only one color and by looking at the second element of the first tuple in clrs you can infer the color.

Is Pil image RGB or BGR?

The basic difference between OpenCV image and PIL image is OpenCV follows BGR color convention and PIL follows RGB color convention and the method of converting will be based on this difference.

How do I know if an image is RGB or Grayscale?

RGB image has 3 channels, grayscale image has 1 channel. @orbit It's in an ideal case. However, there can be a greyscale image with the 3-channel image (RGB) where R == G == B as mentioned in other comments.


3 Answers

if not img.getbbox(): 

... will test to see whether an image is completely black. (Image.getbbox() returns the falsy None if there are no non-black pixels in the image, otherwise it returns a tuple of points, which is truthy.) To test whether an image is completely white, invert it first:

if not ImageChops.invert(img).getbbox(): 

You can also use img.getextrema(). This will tell you the highest and lowest values within the image. To work with this most easily you should probably convert the image to grayscale mode first (otherwise the extrema might be an RGB or RGBA tuple, or a single grayscale value, or an index, and you have to deal with all those).

extrema = img.convert("L").getextrema() if extrema == (0, 0):     # all black elif extrema == (1, 1):     # all white 

The latter method will likely be faster, but not so you'd notice in most applications (both will be quite fast).

A one-line version of the above technique that tests for either black or white:

if sum(img.convert("L").getextrema()) in (0, 2):     # either all black or all white 
like image 50
kindall Avatar answered Sep 24 '22 21:09

kindall


from PIL import Image img = Image.open("test.png") clrs = img.getcolors() 

clrs contains [("num of occurences","color"),...]

By checking for len(clrs) == 1 you can verify if the image contains only one color and by looking at the second element of the first tuple in clrs you can infer the color.

In case the image contains multiple colors, then by taking the number of occurences into account you can also handle almost-completly-single-colored images if 99% of the pixles share the same color.

like image 45
Raffael Avatar answered Sep 20 '22 21:09

Raffael


Expanding on Kindall: if you look at an image called img with:

extrema = img.convert("L").getextrema()

It gives you a range of the values in the images. So an all black image would be (0,0) and an all white image is (255,255). So you can look at:

if extrema[0] == extrema[1]:
    return("This image is one solid color, so I won't use it")
else:
    # do something with the image img
    pass

Useful to me when I was creating a thumbnail from some data and wanted to make sure it was reading correctly.

like image 28
Jeremy S. Avatar answered Sep 21 '22 21:09

Jeremy S.