Skip to content

Commit

Permalink
Only use an RGBA palette for images with an alpha channel
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 27, 2021
1 parent 7005e66 commit 804183c
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Tests/test_file_apng.py
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_image_convert.py
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Expand Up @@ -833,7 +833,7 @@ def load(self):
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:
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/WebPImagePlugin.py
Expand Up @@ -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")

Expand Down

0 comments on commit 804183c

Please sign in to comment.