我正在尝试构建一个模块,可以从图像中提取主色调。例如,我试图从这幅旗帜图像中提取四种颜色:
如果我尝试在RGB模式下使用getpalette
和getcolors
方法,则返回None
。(我试过设置maxcolors
,但仍然得到None
。)
paletted = img.convert('RGB', palette=Image.ADAPTIVE, colors=4)
print(paletted.getpalette())
print(paletted.getcolors(maxcolors=256))
# None
# None
如果我转换为P
模式,我可以使用这些模块,但我会丢失黄色
paletted = img.convert('P', palette=Image.ADAPTIVE, colors=4)
paletted.show()
我做错了什么
首先,以下内容不起作用,或者准确地说,被忽略了:
从关于^{} 的文件中:
您正在从
RGB
转换为RGB
,因此palette
和colors
参数被忽略下一个问题,也会阻止正确转换到模式} 的文件中:
P
:您有一个带有JPG工件的JPG图像。您的图像包含的颜色比您预期的多得多。从关于^{增加
maxcolors
(对于一些完整的24位RGB图像,可能的最大不同颜色数为224),并检查四种最突出的颜色:你会得到纯黑色、近纯蓝色、近纯红色和红色的变体。黄色在哪里?JPG工件!你有很多带黑色、带蓝色和带红色的颜色,它们都比第一种带黄色的颜色更突出。例如,即使设置
n_dom_colors = 20
也不会显示第一种黄色如何实现,你的想法是什么?请看一下^{} ,它将为您提供一个使用颜色量化的模式
P
图像:你会得到近乎纯黑色、近乎纯蓝色、近乎纯红色和黄色的变体
作为参考,量化和重新转换的图像:
相关问题 更多 >
编程相关推荐