Repository: python-pillow/Pillow

Issue:
Animated, transparent GIFs not calculating diff correctly
function `_write_multiple_frames` in `GifImagePlugin.py` has an error on line 445 ([line 445 in present repo](https://github.com/python-pillow/Pillow/blob/daa8b5133923561d2c6d4d1f7b2c9d9aa233007e/src/PIL/GifImagePlugin.py#L445), line 443 in released version 5.4.1).
Specifically, when the disposal_method is set to '2', it should calculate the diff against the background color, but currently it is calculating the diff against the previous frame
`previous = im_frames[-1]`,
and this produces substantial artifacts in the rendering whenever there are still portions of the image in adjacent frames.


Discussion:
Can you make a pull request to fix that?

Resolving patch:
diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py
--- a/src/PIL/GifImagePlugin.py
+++ b/src/PIL/GifImagePlugin.py
@@ -426,6 +426,7 @@ def _write_multiple_frames(im, fp, palette):
 
     im_frames = []
     frame_count = 0
+    background_im = None
     for imSequence in itertools.chain([im], im.encoderinfo.get("append_images", [])):
         for im_frame in ImageSequence.Iterator(imSequence):
             # a copy is required here since seek can still mutate the image
@@ -445,11 +446,22 @@ def _write_multiple_frames(im, fp, palette):
             if im_frames:
                 # delta frame
                 previous = im_frames[-1]
-                if _get_palette_bytes(im_frame) == _get_palette_bytes(previous["im"]):
-                    delta = ImageChops.subtract_modulo(im_frame, previous["im"])
+                if encoderinfo.get("disposal") == 2:
+                    if background_im is None:
+                        background = _get_background(
+                            im,
+                            im.encoderinfo.get("background", im.info.get("background")),
+                        )
+                        background_im = Image.new("P", im_frame.size, background)
+                        background_im.putpalette(im_frames[0]["im"].palette)
+                    base_im = background_im
+                else:
+                    base_im = previous["im"]
+                if _get_palette_bytes(im_frame) == _get_palette_bytes(base_im):
+                    delta = ImageChops.subtract_modulo(im_frame, base_im)
                 else:
                     delta = ImageChops.subtract_modulo(
-                        im_frame.convert("RGB"), previous["im"].convert("RGB")
+                        im_frame.convert("RGB"), base_im.convert("RGB")
                     )
                 bbox = delta.getbbox()
                 if not bbox:
@@ -683,10 +695,12 @@ def _get_color_table_size(palette_bytes):
     # calculate the palette size for the header
     import math
 
-    color_table_size = int(math.ceil(math.log(len(palette_bytes) // 3, 2))) - 1
-    if color_table_size < 0:
-        color_table_size = 0
-    return color_table_size
+    if not palette_bytes:
+        return 0
+    elif len(palette_bytes) < 9:
+        return 1
+    else:
+        return int(math.ceil(math.log(len(palette_bytes) // 3, 2))) - 1
 
 
 def _get_header_palette(palette_bytes):
@@ -717,6 +731,18 @@ def _get_palette_bytes(im):
     return im.palette.palette
 
 
+def _get_background(im, infoBackground):
+    background = 0
+    if infoBackground:
+        background = infoBackground
+        if isinstance(background, tuple):
+            # WebPImagePlugin stores an RGBA value in info["background"]
+            # So it must be converted to the same format as GifImagePlugin's
+            # info["background"] - a global color table index
+            background = im.palette.getcolor(background)
+    return background
+
+
 def _get_global_header(im, info):
     """Return a list of strings representing a GIF header"""
 
@@ -736,14 +762,7 @@ def _get_global_header(im, info):
         if im.info.get("version") == b"89a":
             version = b"89a"
 
-    background = 0
-    if "background" in info:
-        background = info["background"]
-        if isinstance(background, tuple):
-            # WebPImagePlugin stores an RGBA value in info["background"]
-            # So it must be converted to the same format as GifImagePlugin's
-            # info["background"] - a global color table index
-            background = im.palette.getcolor(background)
+    background = _get_background(im, info.get("background"))
 
     palette_bytes = _get_palette_bytes(im)
     color_table_size = _get_color_table_size(palette_bytes)


Test patch:
diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py
--- a/Tests/test_file_gif.py
+++ b/Tests/test_file_gif.py
@@ -1,6 +1,6 @@
 from .helper import unittest, PillowTestCase, hopper, netpbm_available
 
-from PIL import Image, ImagePalette, GifImagePlugin
+from PIL import Image, ImagePalette, GifImagePlugin, ImageDraw
 
 from io import BytesIO
 
@@ -59,7 +59,7 @@ def test_bilevel(optimize):
             return len(test_file.getvalue())
 
         self.assertEqual(test_grayscale(0), 800)
-        self.assertEqual(test_grayscale(1), 38)
+        self.assertEqual(test_grayscale(1), 44)
         self.assertEqual(test_bilevel(0), 800)
         self.assertEqual(test_bilevel(1), 800)
 
@@ -318,6 +318,103 @@ def test_save_dispose(self):
             img.seek(img.tell() + 1)
             self.assertEqual(img.disposal_method, i + 1)
 
+    def test_dispose2_palette(self):
+        out = self.tempfile("temp.gif")
+
+        # 4 backgrounds: White, Grey, Black, Red
+        circles = [(255, 255, 255), (153, 153, 153), (0, 0, 0), (255, 0, 0)]
+
+        im_list = []
+        for circle in circles:
+            img = Image.new("RGB", (100, 100), (255, 0, 0))
+
+            # Red circle in center of each frame
+            d = ImageDraw.Draw(img)
+            d.ellipse([(40, 40), (60, 60)], fill=circle)
+
+            im_list.append(img)
+
+        im_list[0].save(out, save_all=True, append_images=im_list[1:], disposal=2)
+
+        img = Image.open(out)
+
+        for i, circle in enumerate(circles):
+            img.seek(i)
+            rgb_img = img.convert("RGB")
+
+            # Check top left pixel matches background
+            self.assertEqual(rgb_img.getpixel((0, 0)), (255, 0, 0))
+
+            # Center remains red every frame
+            self.assertEqual(rgb_img.getpixel((50, 50)), circle)
+
+    def test_dispose2_diff(self):
+        out = self.tempfile("temp.gif")
+
+        # 4 frames: red/blue, red/red, blue/blue, red/blue
+        circles = [
+            ((255, 0, 0, 255), (0, 0, 255, 255)),
+            ((255, 0, 0, 255), (255, 0, 0, 255)),
+            ((0, 0, 255, 255), (0, 0, 255, 255)),
+            ((255, 0, 0, 255), (0, 0, 255, 255)),
+        ]
+
+        im_list = []
+        for i in range(len(circles)):
+            # Transparent BG
+            img = Image.new("RGBA", (100, 100), (255, 255, 255, 0))
+
+            # Two circles per frame
+            d = ImageDraw.Draw(img)
+            d.ellipse([(0, 30), (40, 70)], fill=circles[i][0])
+            d.ellipse([(60, 30), (100, 70)], fill=circles[i][1])
+
+            im_list.append(img)
+
+        im_list[0].save(
+            out, save_all=True, append_images=im_list[1:], disposal=2, transparency=0
+        )
+
+        img = Image.open(out)
+
+        for i, colours in enumerate(circles):
+            img.seek(i)
+            rgb_img = img.convert("RGBA")
+
+            # Check left circle is correct colour
+            self.assertEqual(rgb_img.getpixel((20, 50)), colours[0])
+
+            # Check right circle is correct colour
+            self.assertEqual(rgb_img.getpixel((80, 50)), colours[1])
+
+            # Check BG is correct colour
+            self.assertEqual(rgb_img.getpixel((1, 1)), (255, 255, 255, 0))
+
+    def test_dispose2_background(self):
+        out = self.tempfile("temp.gif")
+
+        im_list = []
+
+        im = Image.new("P", (100, 100))
+        d = ImageDraw.Draw(im)
+        d.rectangle([(50, 0), (100, 100)], fill="#f00")
+        d.rectangle([(0, 0), (50, 100)], fill="#0f0")
+        im_list.append(im)
+
+        im = Image.new("P", (100, 100))
+        d = ImageDraw.Draw(im)
+        d.rectangle([(0, 0), (100, 50)], fill="#f00")
+        d.rectangle([(0, 50), (100, 100)], fill="#0f0")
+        im_list.append(im)
+
+        im_list[0].save(
+            out, save_all=True, append_images=im_list[1:], disposal=[0, 2], background=1
+        )
+
+        im = Image.open(out)
+        im.seek(1)
+        self.assertEqual(im.getpixel((0, 0)), 0)
+
     def test_iss634(self):
         img = Image.open("Tests/images/iss634.gif")
         # seek to the second frame