我想在Python中将图像的位深度(颜色深度)降低到一组自定义颜色。本质上是给程序一组x颜色,让它将全彩图像中的每个像素与给定列表中最接近的颜色相关联。我的枕头库有问题,如下所示
根据从图像本身创建的调色板,此代码将正确地将图像“Z.png”中的颜色减少到仅4种颜色:
from PIL import Image
colorImage = Image.open("Z.png")
imageWithColorPalette = colorImage.convert("P", palette=Image.ADAPTIVE, colors=4)
imageWithColorPalette.save("Output.png")
from IPython.display import Image
Image('Output.png')
除了我尝试使用自己的调色板外,此代码与此类似。问题在于,此代码返回与上述代码完全相同的图像,似乎只是使用自适应调色板,而忽略了我尝试指定的自定义调色板:
from PIL import Image
pall = [
0, 0, 0,
255, 0, 0,
255, 255, 0,
255, 153, 0,
]
colorImage = Image.open("Z.png")
imageWithColorPalette = colorImage.convert("P", palette=pall, colors=4)
imageWithColorPalette.save("Output.png")
from IPython.display import Image
Image('Output.png')
根据此处的文件: https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.convert 这里呢 https://pillow.readthedocs.io/en/3.0.x/reference/ImagePalette.html。我假设我的pall的格式/大小不正确,或者我需要在convert方法中包含一个矩阵参数。根据文档,矩阵参数是一个可选的转换矩阵。如果给定,应该是包含浮点值的4或12元组。“我不确定如何实现
不管是什么问题,我都很困惑,希望能得到一些帮助
Alternativley,是否有更好的Python库用于此任务,因为我愿意听取建议
我相信这正是你想要的
相关问题 更多 >
编程相关推荐