Skip to content

Commit

Permalink
Convert RGBA palette to RGBA image when saving WebP
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Sep 25, 2023
1 parent 6bbed1a commit ad12cae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Tests/test_file_webp.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,13 @@ def test_duration(self, tmp_path):

with Image.open(out_webp) as reloaded:
assert reloaded.info["duration"] == 1000

def test_roundtrip_rgba_palette(self, tmp_path):
temp_file = str(tmp_path / "temp.webp")
im = Image.new("RGBA", (1, 1)).convert("P")
assert im.mode == "P"
assert im.palette.mode == "RGBA"
im.save(temp_file)

with Image.open(temp_file) as im:
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
7 changes: 1 addition & 6 deletions src/PIL/WebPImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,7 @@ def _save(im, fp, filename):
exact = 1 if im.encoderinfo.get("exact") else 0

if im.mode not in _VALID_WEBP_LEGACY_MODES:
alpha = (
"A" in im.mode
or "a" in im.mode
or (im.mode == "P" and "transparency" in im.info)
)
im = im.convert("RGBA" if alpha else "RGB")
im = im.convert("RGBA" if im.has_transparency_data() else "RGB")

data = _webp.WebPEncode(
im.tobytes(),
Expand Down

0 comments on commit ad12cae

Please sign in to comment.