From fdfa9e8521a0b9436a6324f652c323a2e3a0b21e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 8 Jul 2021 17:08:11 +1000 Subject: [PATCH] If default conversion from P is RGB with transparency, convert to RGBA --- Tests/test_image_convert.py | 12 ++++++++---- src/PIL/Image.py | 4 +++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 5dcdac0e4..b8c20b943 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -42,10 +42,14 @@ def test_default(): im = hopper("P") assert_image(im, "P", im.size) - im = im.convert() - assert_image(im, "RGB", im.size) - im = im.convert() - assert_image(im, "RGB", im.size) + converted_im = im.convert() + assert_image(converted_im, "RGB", im.size) + converted_im = im.convert() + assert_image(converted_im, "RGB", im.size) + + im.info["transparency"] = 0 + converted_im = im.convert() + assert_image(converted_im, "RGBA", im.size) # ref https://github.com/python-pillow/Pillow/issues/274 diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 124ca392b..c70d37cf3 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -914,16 +914,18 @@ class Image: self.load() + has_transparency = self.info.get("transparency") is not None if not mode and self.mode == "P": # determine default mode if self.palette: mode = self.palette.mode else: mode = "RGB" + if mode == "RGB" and has_transparency: + mode = "RGBA" if not mode or (mode == self.mode and not matrix): return self.copy() - has_transparency = self.info.get("transparency") is not None if matrix: # matrix conversion if mode not in ("L", "RGB"):