简体   繁体   English

python中Image包的主要颜色

[英]Dominant colors with Image package in python

I have a transparent image and I am trying to extract major colors out of it using Image module's getcolor() method 我有一个透明的图像,我正在尝试使用Image模块的getcolor()方法从其中提取主要颜色

y = Image.open('img.png')
y.getcolors()
[Out]: [(21841, 0),
(13328, 1),
 (8171, 2),
 (2673, 3),
 (1337, 4),
 (1010, 5),
 (892, 6),
 (519, 7),
 (379, 8),
 (234, 9)]

How do I get actual color values (or names) corresponding to these indexes? 如何获得与这些索引相对应的实际颜色值(或名称)?

在此处输入图片说明

I am not sure whether the following code snippet is what you are finding. 我不确定下面的代码片段是否是您所找到的。 Convert the Image object to RGBA object and use getcolors() as follows. Image对象转换为RGBA对象, getcolors()如下所示使用getcolors()

from PIL import Image

im = Image.open('img.png')
rgba_im = im.convert('RGBA')
print ( rgba_im.getcolors() )

"""
<Output>
[(2673, (218, 215, 209, 255)), (379, (195, 29, 54, 255)), (21841, (208, 208, 209, 0)), (234, (206, 198, 185, 255)), (519, (201, 178, 176, 255)), (1337, (193, 188, 186, 0)), (8171, (182, 176, 174, 0)), (892, (178, 170, 165, 255)), (13328, (168, 26, 41, 255)), (1010, (107, 18, 19, 255))]
"""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM