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


Condition: Not iterate through each pixel!

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

from PIL import Image
img ="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.

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")
    # do something with the image img

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

I tried the Kindall solution ImageChops.invert(img).getbbox() without success, my test images failed.

I had noticed a problem, white should be 255 BUT I have found white images where numeric extrema are (0,0).. why? See the update below.

I have changed Kindall second solution (getextrema), that works right, in a way that doesn’t need image conversion, I wrote a function and verified that works with Grayscale and RGB images both:

def is_monochromatic_image(img):
    extr = img.getextrema()
    a = 0
    for i in extr:
        if isinstance(i, tuple):
            a += abs(i[0] - i[1])
            a = abs(extr[0] - extr[1])
    return a == 0

The img argument is a PIL Image object.
You can also check, with small modifications, if images are black or white.. but you have to decide if “white” is 0 or 255, perhaps you have the definitive answer, I have not. 🙂
Hope useful

UPDATE: I suppose that white images with zeros inside.. may be PNG or other image format with transparency.