If default conversion from P is RGB with transparency, convert to RGBA

This commit is contained in:
Andrew Murray 2021-07-08 17:08:11 +10:00
parent 5fe583598f
commit fdfa9e8521
2 changed files with 11 additions and 5 deletions

View File

@ -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

View File

@ -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"):