从给定的 .png 文件生成 8 位调色板的最佳基于 Python 的库是什么。 就像在 Photoshop 中生成的 .pal 格式一样。
PS:输入 PNG 已经是 8 位格式。 (调色板)
问候
最佳答案
我还没有找到 .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()
关于python - 通过 Python 从 png 文件生成 8 位调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3184821/