Only use an RGBA palette for images with an alpha channel

This commit is contained in:
Andrew Murray 2021-06-27 18:29:02 +10:00
parent 7005e66f00
commit 804183c248
4 changed files with 5 additions and 5 deletions

View File

@ -249,8 +249,8 @@ def test_apng_mode():
assert im.mode == "P"
im.seek(im.n_frames - 1)
im = im.convert("RGBA")
assert im.getpixel((0, 0)) == (0, 255, 0, 255)
assert im.getpixel((64, 32)) == (0, 255, 0, 255)
assert im.getpixel((0, 0)) == (255, 0, 0, 0)
assert im.getpixel((64, 32)) == (255, 0, 0, 0)
with Image.open("Tests/images/apng/mode_palette_1bit_alpha.png") as im:
assert im.mode == "P"

View File

@ -167,7 +167,7 @@ def test_gif_with_rgba_palette_to_p():
with Image.open("Tests/images/hopper.gif") as im:
im.info["transparency"] = 255
im.load()
assert im.palette.mode == "RGBA"
assert im.palette.mode == "RGB"
im_p = im.convert("P")
# Should not raise ValueError: unrecognized raw mode

View File

@ -833,7 +833,7 @@ class Image:
palette_length = self.im.putpalette(mode, arr)
self.palette.dirty = 0
self.palette.rawmode = None
if "transparency" in self.info:
if "transparency" in self.info and mode in ("RGBA", "LA", "PA"):
if isinstance(self.info["transparency"], int):
self.im.putpalettealpha(self.info["transparency"], 0)
else:

View File

@ -320,7 +320,7 @@ def _save(im, fp, filename):
alpha = (
"A" in im.mode
or "a" in im.mode
or (im.mode == "P" and "A" in im.im.getpalettemode())
or (im.mode == "P" and "transparency" in im.info)
)
im = im.convert("RGBA" if alpha else "RGB")