【问题标题】:Count unique colours in an image [duplicate]计算图像中的唯一颜色[重复]
【发布时间】: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!")

到目前为止一切顺利

&gt;&gt; 43 colours

...只有它使用 PIL,而且仅限于相当小的图像尺寸。

我知道this 的问题。然而,它似乎被混淆地标记为重复,因为想要在图像中找到最主要的颜色。

你能像我上面的例子那样用 numpy 或类似的东西来计算较大图像中的颜色吗?

【问题讨论】:

  • 您想要颜色作为可见颜色还是想要 RGB 的唯一像素值?
  • img.getcolors(img.size[0]*img.size[1]) 返回所有颜色的列表,因此您只需要 len(size)
  • @alexzander 一组 RGB 颜色,我可以得到 len() 会很棒。

标签: python image colors


【解决方案1】:

如何使用 numpy.如果您正在寻找不同的 RGB 颜色,您可以使用 np.unique() 正确指定轴以获得所有不同的颜色。然后你可以对得到的数组做 len 来知道图像中有多少种不同的颜色。

【讨论】:

    猜你喜欢
    • 2019-10-29
    • 2010-09-12
    • 2015-12-15
    • 2012-07-07
    • 1970-01-01
    • 2011-10-28
    • 2011-07-06
    • 2020-02-15
    • 1970-01-01
    相关资源
    最近更新 更多