我使用了colorgram库,并得到了RGB颜色代码列表。
[<colorgram.py Color: Rgb(r=80, g=97, b=85), 42.86094708532268%>,
<colorgram.py Color: Rgb(r=34, g=48, b=37), 20.90975231208169%>,
<colorgram.py Color: Rgb(r=29, g=26, b=18), 12.300570275661888%>]
我想像这样提取RGB数字的数量。
[[80, 97, 85], [34, 48, 37], [29, 26, 18]]
但是每个元素的类型是“colorgram.cologram.Color”,并且列表的长度是可变的。我怎样才能得到RGB数字?
[list(c.rgb) for c in l]
其中 l 是您应该做的颜色表列表
示例
import colorgram
l=colorgram.extract("file.png", 6)
colors=[list(c.rgb) for c in l]
[f(x) for x in iterable]
是复合列表语法。所以这是一种从 x 列表计算列表 a f(x) 的方法。
在这里,我所做的是获取 l 中的每个色图 c,并从中计算列表(c.rgb)。
c 开始一个颜色图,c.rgb 是 rgb 颜色或该颜色图(但仍然采用颜色图包特定类型的形式。这也允许通过字段 [c.r, c.g, c.b] 进行访问)。 list(c.rgb) 将这个 rgb 颜色转换为 3 个 int 的列表。
rgb 值存储为命名元组,存储在
.rgb
对象的 Color
属性下。
您只需调用
my_color.rgb
即可提取 RGB 三元组。
如果您想创建一个 RGB 值列表的列表,并将所有
Colors
对象存储在名为 my_colors
的列表中,您可以使用:
rgb_list = [list(color.rgb) for color in my_colors]
import colorgram as cg
ab=cg.extract("image.jpg", 3000000)
color_palette = []
for count in ab:
r = count.rgb.r
g = count.rgb.g
b = count.rgb.b
new_tuple=(r,g,b)
color_palette.append(new_tuple)
print(color_palette)
try this it will work....