我正在使用Python和PIL。
我有RGB图像,我想知道那些只包含一种颜色(例如#FF0000)或一些非常接近的颜色(#FF0000和#FF0001)。
我正在考虑使用直方图,但很难找出具有3个色带的东西,所以我正在寻找更聪明的算法。
有什么想法吗?
ImageStat模块就是答案!谢谢亚伦。 我使用ImageStat.var来获得方差,它可以很好地工作。
这是我的代码:
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)
答案 0 :(得分:5)
试试ImageStat module。如果extrema
返回的值相同,则图像中只有一种颜色。
答案 1 :(得分:0)
首先,您应该定义两种颜色之间的距离。 然后你只需要验证每个像素与你的颜色的距离是否足够小。
答案 2 :(得分:0)
这是一个你可以使用的小片段:
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
也许使用哈希并将元组存储为密钥,并将其出现次数作为值?