From 8af2d7640ead3ddeba9fdc598dc8e72d580dfdfb Mon Sep 17 00:00:00 2001
From: Andrew Murray <radarhere@users.noreply.github.com>
Date: Thu, 24 Oct 2024 23:26:13 +1100
Subject: [PATCH] Pass palette mode to putpalette

---
 Tests/test_file_gif.py    | 6 ++++--
 src/PIL/GifImagePlugin.py | 5 +++--
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py
index 16c8466f3..4eb8c0d02 100644
--- a/Tests/test_file_gif.py
+++ b/Tests/test_file_gif.py
@@ -4,6 +4,7 @@ import warnings
 from collections.abc import Generator
 from io import BytesIO
 from pathlib import Path
+from typing import Any
 
 import pytest
 
@@ -1431,7 +1432,8 @@ def test_saving_rgba(tmp_path: Path) -> None:
         assert reloaded_rgba.load()[0, 0][3] == 0
 
 
-def test_optimizing_p_rgba(tmp_path: Path) -> None:
+@pytest.mark.parametrize("params", ({}, {"disposal": 2, "optimize": False}))
+def test_p_rgba(tmp_path: Path, params: dict[str, Any]) -> None:
     out = str(tmp_path / "temp.gif")
 
     im1 = Image.new("P", (100, 100))
@@ -1443,7 +1445,7 @@ def test_optimizing_p_rgba(tmp_path: Path) -> None:
     im2 = Image.new("P", (100, 100))
     im2.putpalette(data, "RGBA")
 
-    im1.save(out, save_all=True, append_images=[im2])
+    im1.save(out, save_all=True, append_images=[im2], **params)
 
     with Image.open(out) as reloaded:
         assert reloaded.n_frames == 2
diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py
index a7c4f8b2c..47022d584 100644
--- a/src/PIL/GifImagePlugin.py
+++ b/src/PIL/GifImagePlugin.py
@@ -695,8 +695,9 @@ def _write_multiple_frames(
                         )
                         background = _get_background(im_frame, color)
                         background_im = Image.new("P", im_frame.size, background)
-                        assert im_frames[0].im.palette is not None
-                        background_im.putpalette(im_frames[0].im.palette)
+                        first_palette = im_frames[0].im.palette
+                        assert first_palette is not None
+                        background_im.putpalette(first_palette, first_palette.mode)
                     bbox = _getbbox(background_im, im_frame)[1]
                 elif encoderinfo.get("optimize") and im_frame.mode != "1":
                     if "transparency" not in encoderinfo: