From 4df1305073a9c6f535969242e1162b9600d2511f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 26 Jan 2017 15:27:43 +1100 Subject: [PATCH] Only call _get_used_palette_colors when necessary --- PIL/GifImagePlugin.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/PIL/GifImagePlugin.py b/PIL/GifImagePlugin.py index 21d3a642e..f27371181 100644 --- a/PIL/GifImagePlugin.py +++ b/PIL/GifImagePlugin.py @@ -555,12 +555,12 @@ def _get_optimize(im, info): # * 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 + optimise = _FORCE_OPTIMIZE or im.mode == 'L' + if optimise or im.width * im.height < 512 * 512: + used_palette_colors = _get_used_palette_colors(im) + if optimise or (len(used_palette_colors) <= 128 and + max(used_palette_colors) > len(used_palette_colors)): + return used_palette_colors def _get_used_palette_colors(im): used_palette_colors = []