From 86513e0cb1da26b238a300b4a38549ac599ab7a0 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 14 Aug 2026 23:15:46 -0400 Subject: [PATCH] TST: Convert all non-RGB(A) images to RGBA Sometimes, `oxipng` may optimize an image by making it use a palette, and converting that directly to RGB causes a warning from Pillow. In fact, it may be wrong, since there may be transparency in the palette. Instead of checking all image formats, convert everything non-RGB(A) to RGBA first. --- lib/matplotlib/testing/compare.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/testing/compare.py b/lib/matplotlib/testing/compare.py index 92547c3585f6..0beef9ccc421 100644 --- a/lib/matplotlib/testing/compare.py +++ b/lib/matplotlib/testing/compare.py @@ -401,11 +401,16 @@ def calculate_rms(expected_image, actual_image): def _load_image(path): img = Image.open(path) - # In an RGBA image, if the smallest value in the alpha channel is 255, all - # values in it must be 255, meaning that the image is opaque. If so, - # discard the alpha channel so that it may compare equal to an RGB image. - if img.mode != "RGBA" or img.getextrema()[3][0] == 255: - img = img.convert("RGB") + if img.mode != "RGB": + # If we have anything other than RGB(A) (e.g., a paletted image), convert it + # to RGBA. + if img.mode != "RGBA": + img = img.convert("RGBA") + # In an RGBA image, if the smallest value in the alpha channel is 255, all + # values in it must be 255, meaning that the image is opaque. If so, discard the + # alpha channel so that it may compare equal to an RGB image. + if img.getextrema()[3][0] == 255: + img = img.convert("RGB") return np.asarray(img)