Rearranged used_palette_colors to fix get_local_header

This commit is contained in:
Andrew Murray 2015-04-24 00:44:27 +10:00
parent aa1368f551
commit f028928b5a

View File

@ -332,8 +332,12 @@ def get_local_header(fp, im, offset=(0, 0)):
transparency = int(transparency)
# optimize the block away if transparent color is not used
transparent_color_exists = True
if _get_optimize(im, im.encoderinfo):
used_palette_colors = _get_used_palette_colors(im)
# adjust the transparency index after optimize
if used_palette_colors is not None and len(used_palette_colors) < 256:
if len(used_palette_colors) < 256:
for i in range(len(used_palette_colors)):
if used_palette_colors[i] == transparency:
transparency = i
@ -433,11 +437,26 @@ def _save_netpbm(im, fp, filename):
# --------------------------------------------------------------------
# GIF utilities
def _get_optimize(im, info):
return im.mode in ("P", "L") and info and info.get("optimize", 0)
def _get_used_palette_colors(im):
used_palette_colors = []
# check which colors are used
i = 0
for count in im.histogram():
if count:
used_palette_colors.append(i)
i += 1
return used_palette_colors
def getheader(im, palette=None, info=None):
"""Return a list of strings representing a GIF header"""
optimize = info and info.get("optimize", 0)
# Header Block
# http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
header = [
@ -459,15 +478,8 @@ def getheader(im, palette=None, info=None):
used_palette_colors = palette_bytes = None
if im.mode in ("P", "L") and optimize:
used_palette_colors = []
# check which colors are used
i = 0
for count in im.histogram():
if count:
used_palette_colors.append(i)
i += 1
if _get_optimize(im, info):
used_palette_colors = _get_used_palette_colors(im)
# create the new palette if not every color is used
if len(used_palette_colors) < 256: