Python PIL检测图像是否完全黑或白

时间:2021-12-07 06:09:22

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

使用Python图像库PIL,人们如何检测一个图像的所有像素是黑还是白?

~Update~

~更新~

Condition: Not iterate through each pixel!

条件:不遍历每个像素!

3 个解决方案

#1


17  

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:

…将测试一个图像是否完全为黑色。(image .getbbox()如果图像中没有非黑色像素,则返回falsy None,否则返回一组点,这是正确的)。若要测试图像是否完全为白色,请先将其倒置:

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

您还可以使用img.getextrema()。这将告诉您图像中的最大值和最小值。为了最容易地处理这个问题,您应该首先将图像转换为灰度模式(否则,极值可能是RGB或RGBA元组,或者一个灰度值,或者一个索引,您必须处理所有这些)。

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

#2


6  

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

关于Kindall的扩展:如果你看一个名为img的图像:

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:

它给出了图像中值的范围。所以一个全黑图像是(0,0)一个全白图像是(255,255)你可以看看

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

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

当我从一些数据中创建缩略图并希望确保它正确读取时,这对我很有用。

#3


3  

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

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

clrs包含[(“出现的num”,“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.

通过检查len(clrs) = 1,您可以验证图像是否只包含一种颜色,通过查看clrs中第一个元组的第二个元素,您可以推断出颜色。

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.

如果图像包含多种颜色,那么考虑到出现的次数,如果99%的像素共享相同的颜色,那么您也可以处理几乎全单色的图像。

#1


17  

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:

…将测试一个图像是否完全为黑色。(image .getbbox()如果图像中没有非黑色像素,则返回falsy None,否则返回一组点,这是正确的)。若要测试图像是否完全为白色,请先将其倒置:

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

您还可以使用img.getextrema()。这将告诉您图像中的最大值和最小值。为了最容易地处理这个问题,您应该首先将图像转换为灰度模式(否则,极值可能是RGB或RGBA元组,或者一个灰度值,或者一个索引,您必须处理所有这些)。

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

#2


6  

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

关于Kindall的扩展:如果你看一个名为img的图像:

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:

它给出了图像中值的范围。所以一个全黑图像是(0,0)一个全白图像是(255,255)你可以看看

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

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

当我从一些数据中创建缩略图并希望确保它正确读取时,这对我很有用。

#3


3  

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

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

clrs包含[(“出现的num”,“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.

通过检查len(clrs) = 1,您可以验证图像是否只包含一种颜色,通过查看clrs中第一个元组的第二个元素,您可以推断出颜色。

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.

如果图像包含多种颜色,那么考虑到出现的次数,如果99%的像素共享相同的颜色,那么您也可以处理几乎全单色的图像。