2

从给定的 .png 文件生成 8 位调色板的最佳基于 python 的库是什么。就像在 .pal 格式下生成的 Photoshop 一样。

PS:输入 PNG 已经是 8 位格式。(调色板)

问候

4

2 回答 2

3

我无法找到 .PAL 的规范(Photoshop 将其称为“Microsoft PAL”),但该格式很容易进行逆向工程。这有效:

def extractPalette(infile,outfile):
    im=Image.open(infile)
    pal=im.palette.palette
    if im.palette.rawmode!='RGB':
        raise ValueError("Invalid mode in PNG palette")
    output=open(outfile,'wb')
    output.write('RIFF\x10\x04\x00\x00PAL data\x04\x04\x00\x00\x00\x03\x00\x01') # header
    output.write(''.join(pal[i:i+3]+'\0' for i in range(0,768,3))) # convert RGB to RGB0 before writing 
    output.close()
于 2010-07-06T16:45:39.623 回答
1

如果它是一个托盘图像,那么您可以在将getcolors()其加载到 PIL 后使用该方法。如果它是 RGB 或 RGBA 图像,那么您需要进行颜色减少,直到最多有 256 种颜色。

于 2010-07-06T09:03:27.083 回答