Python如何获取一个图像中使用的颜色列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4643847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:43:39  来源:igfitidea点击:

Python how to get a list of color that used in one image

pythonpython-imaging-library

提问by user469652

Python how to get a list of color that used in one image

Python如何获取一个图像中使用的颜色列表

I use PIL, and I want to have a dictionary of colors that are used in this image, including color(key) and number of pixel points it used.

我使用 PIL,并且我想要一个包含此图像中使用的颜色的字典,包括颜色(键)和它使用的像素点数。

How to do that?

怎么做?

采纳答案by unmounted

I have used something like the following a few times to analyze graphs:

我曾多次使用以下内容来分析图表:

>>> from PIL import Image
>>> im = Image.open('polar-bear-cub.jpg')
>>> from collections import defaultdict
>>> by_color = defaultdict(int)
>>> for pixel in im.getdata():
...     by_color[pixel] += 1
>>> by_color
defaultdict(<type 'int'>, {(11, 24, 41): 8, (53, 52, 58): 8, (142, 147, 117): 1, (121, 111, 119): 1, (234, 228, 216): 4

Ie, there are 8 pixels with rbg value (11, 24, 41), and so on.

即,有 8 个像素的 rbg 值(11、24、41),依此类推。

回答by Jake

The getcolors method should do the trick. See the docs.

getcolors 方法应该可以解决问题。请参阅文档

Edit: That link is broken. Pillow seems to be the go-to lib now, forked from PIL. New Docs

编辑:该链接已损坏。Pillow 现在似乎是首选库,从 PIL 分叉出来。新文档

Image.open('file.jpg').getcolors() => a list of (count, color) tuples or None

回答by Wouter Mol

I'd like to add that the .getcolors() function only works if the image is in an RGB mode of some sort.

我想补充一点,.getcolors() 函数仅在图像处于某种 RGB 模式时才有效。

I had this problem where it would return a list of tuples with (count, color) where color was just a number. Took me a while to find it, but this fixed it.

我遇到了这个问题,它会返回一个带有 (count, color) 的元组列表,其中颜色只是一个数字。我花了一段时间才找到它,但这修复了它。

from PIL import Image
img = Image.open('image.png')
colors = img.convert('RGB').getcolors() #this converts the mode to RGB

回答by Eyal Enav

See https://github.com/fengsp/color-thief-py"Grabs the dominant color or a representative color palette from an image. Uses Python and Pillow"

请参阅https://github.com/fengsp/color-thief-py“从图像中获取主色或代表性调色板。使用 Python 和枕头”

from colorthief import ColorThief

color_thief = ColorThief('/path/to/imagefile')
# get the dominant color
dominant_color = color_thief.get_color(quality=1)
# build a color palette
palette = color_thief.get_palette(color_count=6)

回答by Mikhail Gerasimov

getcolors()returnsNoneif number of colors in the image is greater than maxcolorargument. The function also works only with 'RGB' images. It's not very convenient.

getcolors()返回None如果颜色的图像中的数量大于maxcolor参数。该功能也仅适用于“RGB”图像。这不是很方便。

getdata()on the other hand can be utilized in very convenient way together with Counter:

getdata()另一方面可以以非常方便的方式与Counter一起使用:

from collections import Counter


colors = Counter(image.getdata())   # dict: color -> number

set(colors)                  # set of unique colors  
len(colors)                  # number of unique colors 
colors[(0, 0, 0)]            # black color frequency
max(colors, key=colors.get)  # most frequent color