python - 通过 Python 从 png 文件生成 8 位调色板

标签 python png palette color-palette

从给定的 .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/

相关文章:

python - 陷入 Scapy 文档中的 VarLenQField 示例

image - 如何将 PNG 转换为 1 位 BMP

android - 在包 'srcCompat' 中找不到属性 '....' 的资源标识符

android - 带有 Picasso 的 ListView Android 中的调色板

python - 执行 sqlalchemy 存在查询

python - if 语句后不打印

swift - PNG 图像未保存到文档目录

java - 如何将Bufferedimage转换为索引类型,然后提取argb调色板

python - Bokeh 线图 : How to assign colors when plotting a large number of lines

python - 在python中从字符串创建变量