Added possibility to save gifs with a custom palette when using color mode P

This commit is contained in:
d-schmidt 2013-01-10 20:34:58 +01:00
parent 45a97bb430
commit 900f3a8ff7

View File

@ -248,7 +248,12 @@ def _save(im, fp, filename):
rawmode = "L" rawmode = "L"
# header # header
for s in getheader(imOut, im.encoderinfo): try:
palette = im.encoderinfo["palette"]
except KeyError:
palette = None
for s in getheader(imOut, palette, im.encoderinfo):
fp.write(s) fp.write(s)
flags = 0 flags = 0
@ -319,7 +324,7 @@ def _save_netpbm(im, fp, filename):
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# GIF utilities # GIF utilities
def getheader(im, info=None): def getheader(im, palette, info=None):
"""Return a list of strings representing a GIF header""" """Return a list of strings representing a GIF header"""
optimize = info and info.get("optimize", 0) optimize = info and info.get("optimize", 0)
@ -347,7 +352,13 @@ def getheader(im, info=None):
# global palette # global palette
if im.mode == "P": if im.mode == "P":
# colour palette # colour palette
s.append(im.im.getpalette("RGB")[:maxcolor*3]) if palette is not None and Image.isBytesType(palette):
paletteBytes = palette
else:
paletteBytes =im.im.getpalette("RGB")[:maxcolor*3]
s.append(paletteBytes)
else: else:
# greyscale # greyscale
for i in range(maxcolor): for i in range(maxcolor):