本文介绍了如何检查RGB图像是否只包含一种颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是Python和PIL。
我有RGB图像,我想知道那些只包含一种颜色(例如#FF0000)或一些非常接近的颜色(#FF0000和#FF0001)。
我在想使用直方图,但是很难弄清楚3个颜色带
这是我的代码: p>
来自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
解决方案
尝试。如果
extrema
返回的值相同,则图片中只有一种颜色。
I'm using Python and 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).
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.
Any ideas?
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)
解决方案 Try the ImageStat module. If the values returned by extrema
are the same, you have only a single color in the image.
这篇关于如何检查RGB图像是否只包含一种颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!