使用python PIL更改8位.png图像的调色板

Jac*_* Ha 4 python image-processing python-imaging-library

我正在寻找一种快速方法将新调色板应用于现有的8位.png图像.我怎样才能做到这一点?保存图像时,.png是否重新编码?(自己回答:看来是这样)

我尝试过(编辑过):

import Image, ImagePalette
output = StringIO.StringIO()
palette = (.....) #long palette of 768 items
im = Image.open('test_palette.png') #8 bit image
im.putpalette(palette) 
im.save(output, format='PNG')
Run Code Online (Sandbox Code Playgroud)

通过我的testimage,保存功能大约需要65毫安.我的想法:没有解码和编码,它可以更快?

The*_*ran 6

如果你只想改变调色板,那么PIL就会妨碍你.幸运的是,当您只对某些数据块感兴趣时,PNG文件格式设计为易于处理.PLTE块的格式只是一个RGB三元组阵列,最后有一个CRC.要在不读取或写入整个文件的情况下就地更改文件上的调色板:

import struct
from zlib import crc32
import os

# PNG file format signature
pngsig = '\x89PNG\r\n\x1a\n'

def swap_palette(filename):
    # open in read+write mode
    with open(filename, 'r+b') as f:
        f.seek(0)
        # verify that we have a PNG file
        if f.read(len(pngsig)) != pngsig:
            raise RuntimeError('not a png file!')

        while True:
            chunkstr = f.read(8)
            if len(chunkstr) != 8:
                # end of file
                break

            # decode the chunk header
            length, chtype = struct.unpack('>L4s', chunkstr)
            # we only care about palette chunks
            if chtype == 'PLTE':
                curpos = f.tell()
                paldata = f.read(length)
                # change the 3rd palette entry to cyan
                paldata = paldata[:6] + '\x00\xff\xde' + paldata[9:]

                # go back and write the modified palette in-place
                f.seek(curpos)
                f.write(paldata)
                f.write(struct.pack('>L', crc32(chtype+paldata)&0xffffffff))
            else:
                # skip over non-palette chunks
                f.seek(length+4, os.SEEK_CUR)

if __name__ == '__main__':
    import shutil
    shutil.copyfile('redghost.png', 'blueghost.png')
    swap_palette('blueghost.png')
Run Code Online (Sandbox Code Playgroud)

此代码将redghost.png复制到blueghost.png并在原位修改blueghost.png的调色板.

红鬼 - > 蓝鬼