【发布时间】:2021-03-02 19:40:31
【问题描述】:
我正在尝试计算图像中(唯一)颜色的数量。这就是我所拥有的:
from PIL import Image
myImage = "D:\\temp\\tiny_pal.png"
img = Image.open(myImage)
size = img.getcolors(img.size[0]*img.size[1])
if len(size) < 256:
colours = img.convert('RGB').getcolors() # this converts the mode to RGB
print str(len(colours)) + " colours"
else:
print ("Image is too big!")
到目前为止一切顺利
>> 43 colours
...只有它使用 PIL,而且仅限于相当小的图像尺寸。
我知道this 的问题。然而,它似乎被混淆地标记为重复,因为想要在图像中找到最主要的颜色。
你能像我上面的例子那样用 numpy 或类似的东西来计算较大图像中的颜色吗?
【问题讨论】:
-
您想要颜色作为可见颜色还是想要 RGB 的唯一像素值?
-
img.getcolors(img.size[0]*img.size[1])返回所有颜色的列表,因此您只需要len(size)。 -
@alexzander 一组 RGB 颜色,我可以得到
len()会很棒。