Fixed palette issue when saving

This commit is contained in:
Andrew Murray 2015-08-24 23:23:07 +10:00
parent b8ff91ab3b
commit 88fca0f555

View File

@ -301,14 +301,17 @@ RAWMODE = {
} }
def _convert_mode(im): def _convert_mode(im, initial_call=False):
# convert on the fly (EXPERIMENTAL -- I'm not sure PIL # convert on the fly (EXPERIMENTAL -- I'm not sure PIL
# should automatically convert images on save...) # should automatically convert images on save...)
if Image.getmodebase(im.mode) == "RGB": if Image.getmodebase(im.mode) == "RGB":
if initial_call:
palette_size = 256 palette_size = 256
if im.palette: if im.palette:
palette_size = len(im.palette.getdata()[1]) // 3 palette_size = len(im.palette.getdata()[1]) // 3
return im.convert("P", palette=1, colors=palette_size) return im.convert("P", palette=1, colors=palette_size)
else:
return im.convert("P")
return im.convert("L") return im.convert("L")
@ -330,7 +333,7 @@ def _save(im, fp, filename, save_all=False):
if im.mode in RAWMODE: if im.mode in RAWMODE:
im_out = im.copy() im_out = im.copy()
else: else:
im_out = _convert_mode(im) im_out = _convert_mode(im, True)
# header # header
try: try: