Convert RGBA palette to RGBA image when saving WebP

This commit is contained in:
Andrew Murray 2023-09-25 20:28:25 +10:00
parent 6bbed1add0
commit ad12caecda
2 changed files with 11 additions and 6 deletions

View File

@ -234,3 +234,13 @@ class TestFileWebp:
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)

View File

@ -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(),