mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-08 05:43:12 +03:00
Rearranged used_palette_colors to fix get_local_header
This commit is contained in:
parent
aa1368f551
commit
f028928b5a
|
@ -332,15 +332,19 @@ def get_local_header(fp, im, offset=(0, 0)):
|
||||||
transparency = int(transparency)
|
transparency = int(transparency)
|
||||||
# optimize the block away if transparent color is not used
|
# optimize the block away if transparent color is not used
|
||||||
transparent_color_exists = True
|
transparent_color_exists = True
|
||||||
# adjust the transparency index after optimize
|
|
||||||
if used_palette_colors is not None and len(used_palette_colors) < 256:
|
if _get_optimize(im, im.encoderinfo):
|
||||||
for i in range(len(used_palette_colors)):
|
used_palette_colors = _get_used_palette_colors(im)
|
||||||
if used_palette_colors[i] == transparency:
|
|
||||||
transparency = i
|
# adjust the transparency index after optimize
|
||||||
transparent_color_exists = True
|
if len(used_palette_colors) < 256:
|
||||||
break
|
for i in range(len(used_palette_colors)):
|
||||||
else:
|
if used_palette_colors[i] == transparency:
|
||||||
transparent_color_exists = False
|
transparency = i
|
||||||
|
transparent_color_exists = True
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
transparent_color_exists = False
|
||||||
|
|
||||||
if "duration" in im.encoderinfo:
|
if "duration" in im.encoderinfo:
|
||||||
duration = int(im.encoderinfo["duration"] / 10)
|
duration = int(im.encoderinfo["duration"] / 10)
|
||||||
|
@ -433,11 +437,26 @@ def _save_netpbm(im, fp, filename):
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# GIF utilities
|
# 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):
|
def getheader(im, palette=None, 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)
|
|
||||||
|
|
||||||
# Header Block
|
# Header Block
|
||||||
# http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
|
# http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
|
||||||
header = [
|
header = [
|
||||||
|
@ -459,15 +478,8 @@ def getheader(im, palette=None, info=None):
|
||||||
|
|
||||||
used_palette_colors = palette_bytes = None
|
used_palette_colors = palette_bytes = None
|
||||||
|
|
||||||
if im.mode in ("P", "L") and optimize:
|
if _get_optimize(im, info):
|
||||||
used_palette_colors = []
|
used_palette_colors = _get_used_palette_colors(im)
|
||||||
|
|
||||||
# check which colors are used
|
|
||||||
i = 0
|
|
||||||
for count in im.histogram():
|
|
||||||
if count:
|
|
||||||
used_palette_colors.append(i)
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
# create the new palette if not every color is used
|
# create the new palette if not every color is used
|
||||||
if len(used_palette_colors) < 256:
|
if len(used_palette_colors) < 256:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user