问题描述
使用Python Imaging Library,我可以调用
Using the Python Imaging Library, I can call
img.convert("P", palette=Image.ADAPTIVE)
或
img.convert("P", palette=Image.WEB)
但是有一种转换为任意调色板的方法?
but is there a way to convert to an arbitrary palette?
p = []
for i in range(0, 256):
p.append(i, 0, 0)
img.convert("P", palette=p)
它会将每个像素映射到图像中最接近的颜色吗?或者这是否支持 Image.WEB
而没有别的?
where it'll map each pixel to the closest colour found in the image? Or is this supported for Image.WEB
and nothing else?
推荐答案
在查看 convert()
的源代码时,我看到它引用了 im.quantize
。
quantze
可以采用调色板参数。如果您提供具有调色板的图像,此功能将采用该调色板并将其应用于图像。
While looking through the source code of convert()
I saw that it references im.quantize
.quantize
can take a palette argument. If you provide an Image that has a palette, this function will take that palette and apply it to the image.
示例:
src = Image.open("sourcefilewithpalette.bmp")
new = Image.open("unconvertednew24bit.bmp")
converted = new.quantize(palette=src)
converted.save("converted.bmp")
其他提供的答案对我不起作用(它做了一些非常糟糕的双调色板转换或其他东西,)但是这个解决方案确实如此。
The other provided answer didn't work for me (it did some really bad double palette conversion or something,) but this solution did.
这篇关于PIL:将RGB图像转换为特定的8位调色板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!