From 53de81f0a187fe5b0ede558d65db8dd25cae6f16 Mon Sep 17 00:00:00 2001 From: Moriyoshi Koizumi Date: Thu, 6 Nov 2014 12:18:41 +0900 Subject: [PATCH 1/3] The optimization algorithm is only applicable to the image of mode="P". --- PIL/GifImagePlugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/GifImagePlugin.py b/PIL/GifImagePlugin.py index 0b154f417..35dbc5c68 100644 --- a/PIL/GifImagePlugin.py +++ b/PIL/GifImagePlugin.py @@ -433,7 +433,7 @@ def getheader(im, palette=None, info=None): usedPaletteColors = paletteBytes = None - if optimize: + if im.mode == "P" and optimize: usedPaletteColors = [] # check which colors are used From 9570864163bfbd7f673296a1898a5b9f406affd9 Mon Sep 17 00:00:00 2001 From: Moriyoshi Koizumi Date: Thu, 6 Nov 2014 19:08:02 +0900 Subject: [PATCH 2/3] Grayscale images can also be optimized... --- PIL/GifImagePlugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/GifImagePlugin.py b/PIL/GifImagePlugin.py index 35dbc5c68..55aece38c 100644 --- a/PIL/GifImagePlugin.py +++ b/PIL/GifImagePlugin.py @@ -433,7 +433,7 @@ def getheader(im, palette=None, info=None): usedPaletteColors = paletteBytes = None - if im.mode == "P" and optimize: + if im.mode in ("P", "L") and optimize: usedPaletteColors = [] # check which colors are used From d00c3c02adcdf26ebd35904b67c7a66fd23bf154 Mon Sep 17 00:00:00 2001 From: Moriyoshi Koizumi Date: Thu, 6 Nov 2014 19:10:36 +0900 Subject: [PATCH 3/3] Enhance the test for the fix. --- Tests/test_file_gif.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 200b48372..57cd71ac6 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -28,13 +28,22 @@ class TestFileGif(PillowTestCase): def test_optimize(self): from io import BytesIO - def test(optimize): + def test_grayscale(optimize): im = Image.new("L", (1, 1), 0) file = BytesIO() im.save(file, "GIF", optimize=optimize) return len(file.getvalue()) - self.assertEqual(test(0), 800) - self.assertEqual(test(1), 38) + + def test_bilevel(optimize): + im = Image.new("1", (1, 1), 0) + file = BytesIO() + im.save(file, "GIF", optimize=optimize) + return len(file.getvalue()) + + self.assertEqual(test_grayscale(0), 800) + self.assertEqual(test_grayscale(1), 38) + self.assertEqual(test_bilevel(0), 800) + self.assertEqual(test_bilevel(1), 800) def test_optimize_full_l(self): from io import BytesIO