Skip to content

Commit

Permalink
Merge pull request #5594 from radarhere/convert
Browse files Browse the repository at this point in the history
If default conversion from P is RGB with transparency, convert to RGBA
  • Loading branch information
hugovk committed Aug 6, 2021
2 parents b8b4bf4 + fdfa9e8 commit 3307bf6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 8 additions & 4 deletions Tests/test_image_convert.py
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/PIL/Image.py
Expand Up @@ -914,16 +914,18 @@ def convert(self, mode=None, matrix=None, dither=None, palette=WEB, colors=256):

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"):
Expand Down

0 comments on commit 3307bf6

Please sign in to comment.