From 0a475f29cae15e44f4eba3cfd89770ddbca7e8ef Mon Sep 17 00:00:00 2001 From: linnil1 Date: Tue, 31 Jan 2017 03:34:48 +0800 Subject: [PATCH 1/4] Add disposal option --- PIL/GifImagePlugin.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/PIL/GifImagePlugin.py b/PIL/GifImagePlugin.py index 2e519c7ac..aa3db4703 100644 --- a/PIL/GifImagePlugin.py +++ b/PIL/GifImagePlugin.py @@ -453,15 +453,24 @@ def _get_local_header(fp, im, offset, flags): duration = int(im.encoderinfo["duration"] / 10) else: duration = 0 + + if "disposal" in im.encoderinfo: + disposal = int(im.encoderinfo['disposal']) + if not 0<= disposal <=3: + disposal = 2 + else: + disposal = 2 + if transparent_color_exists or duration != 0: - transparency_flag = 1 if transparent_color_exists else 0 + packed_flag = 1 if transparent_color_exists else 0 + packed_flag |= disposal << 2 if not transparent_color_exists: transparency = 0 fp.write(b"!" + o8(249) + # extension intro o8(4) + # length - o8(transparency_flag) + # packed fields + o8(packed_flag) + # packed fields o16(duration) + # duration o8(transparency) + # transparency index o8(0)) From b4a63c1c2003bd27ed87753ee3aba67e5104573c Mon Sep 17 00:00:00 2001 From: linnil1 Date: Tue, 31 Jan 2017 03:55:49 +0800 Subject: [PATCH 2/4] Add dispoal description in documents --- docs/handbook/image-file-formats.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 21339e75f..9e8974df0 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -127,6 +127,14 @@ are available:: **palette** Use the specified palette for the saved image. +**disposal** + Indicates the way in which the graphic is to be treated after being displayed. + + * 0 - No disposal specified. + * 1 - Do not dispose. + * 2 - Restore to background color. + * 3 - Restore to previous content. + Reading local images ~~~~~~~~~~~~~~~~~~~~ From 4c9143ef2d39a22eedac505f288f05b274322916 Mon Sep 17 00:00:00 2001 From: linnil1 Date: Tue, 31 Jan 2017 15:22:54 +0800 Subject: [PATCH 3/4] Add dispose test --- Tests/test_file_gif.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index d987f6851..96c07106b 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -259,6 +259,28 @@ class TestFileGif(PillowTestCase): except EOFError: pass + def test_save_dispose(self): + out = self.tempfile('temp.gif') + im_list = [ + Image.new('L', (100, 100), '#000'), + Image.new('L', (100, 100), '#111'), + Image.new('L', (100, 100), '#222'), + ] + for method in range(0,4): + im_list[0].save( + out, + save_all=True, + append_images=im_list[1:], + disposal=method + ) + img = Image.open(out) + try: + while True: + img.seek(img.tell() + 1) + self.assertEqual(img.disposal_method, method) + except EOFError: + pass + def test_iss634(self): img = Image.open("Tests/images/iss634.gif") # seek to the second frame From fa8c4b3551d99072516ad59b69e7cf07e4e9c90c Mon Sep 17 00:00:00 2001 From: linnil1 Date: Tue, 31 Jan 2017 15:32:01 +0800 Subject: [PATCH 4/4] Thanks @wiredfool sugguest for disposal --- PIL/GifImagePlugin.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/PIL/GifImagePlugin.py b/PIL/GifImagePlugin.py index aa3db4703..a2db9fcf2 100644 --- a/PIL/GifImagePlugin.py +++ b/PIL/GifImagePlugin.py @@ -454,14 +454,9 @@ def _get_local_header(fp, im, offset, flags): else: duration = 0 - if "disposal" in im.encoderinfo: - disposal = int(im.encoderinfo['disposal']) - if not 0<= disposal <=3: - disposal = 2 - else: - disposal = 2 + disposal = int(im.encoderinfo.get('disposal', 0)) - if transparent_color_exists or duration != 0: + if transparent_color_exists or duration != 0 or disposal: packed_flag = 1 if transparent_color_exists else 0 packed_flag |= disposal << 2 if not transparent_color_exists: