I'm using Python and PIL.
我正在使用Python和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).
我有RGB图像,我想知道那些只包含一种颜色(例如#FF0000)或几种非常接近的颜色(#FF0000和#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.
我正在考虑使用直方图,但很难找出具有3个色带的东西,所以我正在寻找更聪明的算法。
Any ideas?
ImageStat module is THE answer! Thanks Aaron. I use ImageStat.var to get the variance and it works perfectly.
ImageStat模块就是答案!谢谢亚伦。我使用ImageStat.var来获得方差,它完美地运行。
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)
3 个解决方案
#1
4
Try the ImageStat module. If the values returned by extrema
are the same, you have only a single color in the image.
试试ImageStat模块。如果extrema返回的值相同,则图像中只有一种颜色。
#2
0
First, you should define a distance between two colors. Then you just have to verify for each pixel that it's distance to your color is small enough.
首先,您应该定义两种颜色之间的距离。然后你只需要验证每个像素与你的颜色的距离是否足够小。
#3
0
Here's a little snippet you could make use of :
这是一个你可以使用的小片段:
import Image
im = Image.open("path_to_image")
width,height = im.size
for w in range(0,width):
for h in range(0,height):
# this will hold the value of all the channels
color_tuple = im.getpixel((w,h))
# do something with the colors here
Maybe use a hash and store the tuples as the key and it's number of appearances as value?
也许使用哈希并将元组存储为密钥,并将它的出现次数作为值?
#1
4
Try the ImageStat module. If the values returned by extrema
are the same, you have only a single color in the image.
试试ImageStat模块。如果extrema返回的值相同,则图像中只有一种颜色。
#2
0
First, you should define a distance between two colors. Then you just have to verify for each pixel that it's distance to your color is small enough.
首先,您应该定义两种颜色之间的距离。然后你只需要验证每个像素与你的颜色的距离是否足够小。
#3
0
Here's a little snippet you could make use of :
这是一个你可以使用的小片段:
import Image
im = Image.open("path_to_image")
width,height = im.size
for w in range(0,width):
for h in range(0,height):
# this will hold the value of all the channels
color_tuple = im.getpixel((w,h))
# do something with the colors here
Maybe use a hash and store the tuples as the key and it's number of appearances as value?
也许使用哈希并将元组存储为密钥,并将它的出现次数作为值?