When saving GIF frame that restores to background color, do not fill identical pixels by radarhere · Pull Request #7788 · python-pillow/Pillow

In my first commit here, I used the previous frame's disposal when considered whether the current frame should be compared to the background color or not.

However, the current frame's disposal was being used when generating background color frame for comparison.

if encoderinfo.get("disposal") == 2:
if background_im is None:
color = im.encoderinfo.get(
"transparency", im.info.get("transparency", (0, 0, 0))
)
background = _get_background(im_frame, color)
background_im = Image.new("P", im_frame.size, background)
background_im.putpalette(im_frames[0]["im"].palette)
delta, bbox = _getbbox(background_im, im_frame)

This is incorrect, and so I've pushed another commit with a new test. Disposal refers to how the current frame will be treated when you have moved on to the next one - how the current frame is disposed of.