From 0b2fa3f952b6b97a121a3d7f05a0753b52b802cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sat, 28 Jun 2025 11:39:49 +0200 Subject: [PATCH 1/2] BUG: fix future incompatibility with Pillow 13 --- lib/matplotlib/backends/_backend_tk.py | 2 +- lib/matplotlib/backends/backend_pdf.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index 0bbff1379ffa..eaf868fd8bec 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -775,7 +775,7 @@ def _recolor_icon(image, color): image_data = np.asarray(image).copy() black_mask = (image_data[..., :3] == 0).all(axis=-1) image_data[black_mask, :3] = color - return Image.fromarray(image_data, mode="RGBA") + return Image.fromarray(image_data) # Use the high-resolution (48x48 px) icon if it exists and is needed with Image.open(path_large if (size > 24 and path_large.exists()) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index f20bdffd4a3a..4429dc9ba707 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1784,7 +1784,8 @@ def _writeImg(self, data, id, smask=None): data[:, :, 2]) indices = np.argsort(palette24).astype(np.uint8) rgb8 = indices[np.searchsorted(palette24, rgb24, sorter=indices)] - img = Image.fromarray(rgb8, mode='P') + img = Image.fromarray(rgb8) + img.convert("P") img.putpalette(palette) png_data, bit_depth, palette = self._writePng(img) if bit_depth is None or palette is None: From 6ede069136c6e9c7f1bceb9b1d4e264ba928aa55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Mon, 30 Jun 2025 13:41:34 +0200 Subject: [PATCH 2/2] fixup Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/backends/backend_pdf.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 4429dc9ba707..a75a8a86eb92 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1784,8 +1784,7 @@ def _writeImg(self, data, id, smask=None): data[:, :, 2]) indices = np.argsort(palette24).astype(np.uint8) rgb8 = indices[np.searchsorted(palette24, rgb24, sorter=indices)] - img = Image.fromarray(rgb8) - img.convert("P") + img = Image.fromarray(rgb8).convert("P") img.putpalette(palette) png_data, bit_depth, palette = self._writePng(img) if bit_depth is None or palette is None: