Merge pull request #2313 from wiredfool/pr_2196

Unified different GIF optimize conditions #2196, Test for #2196
This commit is contained in:
wiredfool 2016-12-27 11:53:50 +00:00 committed by GitHub
commit f36a04570f
2 changed files with 98 additions and 77 deletions

View File

@ -434,13 +434,11 @@ def _get_local_header(fp, im, offset, flags):
# 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)
used_palette_colors = _get_optimize(im, im.encoderinfo)
if used_palette_colors is not None:
# adjust the transparency index after optimize
if len(used_palette_colors) < 256:
for i in range(len(used_palette_colors)):
if used_palette_colors[i] == transparency:
for i, palette_color in enumerate(used_palette_colors):
if palette_color == transparency:
transparency = i
transparent_color_exists = True
break
@ -552,9 +550,28 @@ 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)
# Force optimization so that we can test performance against
# cases where it took lots of memory and time previously.
_FORCE_OPTIMIZE = False
def _get_optimize(im, info):
if im.mode in ("P", "L") and info and info.get("optimize", 0):
# Potentially expensive operation.
# The palette saves 3 bytes per color not used, but palette
# lengths are restricted to 3*(2**N) bytes. Max saving would
# be 768 -> 6 bytes if we went all the way down to 2 colors.
# * If we're over 128 colors, we can't save any space.
# * If there aren't any holes, it's not worth collapsing.
# * If we have a 'large' image, the palette is in the noise.
# create the new palette if not every color is used
used_palette_colors = _get_used_palette_colors(im)
if _FORCE_OPTIMIZE or im.mode == 'L' or \
(len(used_palette_colors) <= 128 and
max(used_palette_colors) > len(used_palette_colors) and
im.width * im.height < 512 * 512):
return used_palette_colors
def _get_used_palette_colors(im):
used_palette_colors = []
@ -586,10 +603,6 @@ def _get_header_palette(palette_bytes):
palette_bytes += o8(0) * 3 * actual_target_size_diff
return palette_bytes
# Force optimization so that we can test performance against
# cases where it took lots of memory and time previously.
_FORCE_OPTIMIZE = False
def _get_palette_bytes(im, palette, info):
if im.mode == "P":
if palette and isinstance(palette, bytes):
@ -602,25 +615,10 @@ def _get_palette_bytes(im, palette, info):
else:
source_palette = bytearray(i//3 for i in range(768))
used_palette_colors = palette_bytes = None
palette_bytes = None
if _get_optimize(im, info):
used_palette_colors = _get_used_palette_colors(im)
# Potentially expensive operation.
# The palette saves 3 bytes per color not used, but palette
# lengths are restricted to 3*(2**N) bytes. Max saving would
# be 768 -> 6 bytes if we went all the way down to 2 colors.
# * If we're over 128 colors, we can't save any space.
# * If there aren't any holes, it's not worth collapsing.
# * If we have a 'large' image, the palette is in the noise.
# create the new palette if not every color is used
if _FORCE_OPTIMIZE or im.mode == 'L' or \
(len(used_palette_colors) <= 128 and
max(used_palette_colors) > len(used_palette_colors) and
im.width * im.height < 512 * 512):
used_palette_colors = _get_optimize(im, info)
if used_palette_colors is not None:
palette_bytes = b""
new_positions = [0]*256

View File

@ -362,5 +362,28 @@ class TestFileGif(PillowTestCase):
reread = Image.open(out)
self.assertEqual(reread.n_frames, 10)
def test_transparent_optimize(self):
# from issue #2195, if the transparent color is incorrectly
# optimized out, gif loses transparency Need a palette that
# isn't using the 0 color, and one that's > 128 items where
# the transparent color is actually the top palette entry to
# trigger the bug.
from PIL import ImagePalette
data = bytes(bytearray(range(1,254)))
palette = ImagePalette.ImagePalette("RGB", list(range(256))*3)
im = Image.new('L', (253,1))
im.frombytes(data)
im.putpalette(palette)
out = self.tempfile('temp.gif')
im.save(out, transparency=253)
reloaded = Image.open(out)
self.assertEqual(reloaded.info['transparency'], 253)
if __name__ == '__main__':
unittest.main()