Repository: python-pillow/Pillow

Issue:
Add webp export option alpha_quality
`cwebp` has a commandline flag of `-alpha_q <x>%` which uses lossy-compression for the alpha channel and can significantly reduce image size, and also seems to speed up image conversion in some cases. It would be great to have this ability in Pillow also.

Additionally an option similar to `-noalpha` where we know the image does not contain any alpha layer saves ~27 bytes per image - not particularly significant but again a nice-to-have for cwebp compatibility.

Thanks!


Discussion:
`noalpha` isn't mentioned in https://developers.google.com/speed/webp/docs/api or https://developers.google.com/speed/webp/docs/container-api, or anything similar, so I don't think this is a setting that we can use.

If you want to ensure that your Pillow image does not have transparency, that seems like just a matter of making sure the mode isn't one of "LA", "La", "PA", "RGBA" or "RGBa", making sure "transparency" isn't in the image's `info`, and making sure that it doesn't have a palette with an alpha channel.
I've created https://github.com/python-pillow/Pillow/pull/7872 to resolve this by adding `alpha_quality`.
Oh brilliant thank you this looks great :-) 

Resolving patch:
diff --git a/src/PIL/WebPImagePlugin.py b/src/PIL/WebPImagePlugin.py
--- a/src/PIL/WebPImagePlugin.py
+++ b/src/PIL/WebPImagePlugin.py
@@ -217,6 +217,7 @@ def _save_all(im, fp, filename):
     verbose = False
     lossless = im.encoderinfo.get("lossless", False)
     quality = im.encoderinfo.get("quality", 80)
+    alpha_quality = im.encoderinfo.get("alpha_quality", 100)
     method = im.encoderinfo.get("method", 0)
     icc_profile = im.encoderinfo.get("icc_profile") or ""
     exif = im.encoderinfo.get("exif", "")
@@ -296,6 +297,7 @@ def _save_all(im, fp, filename):
                     rawmode,
                     lossless,
                     quality,
+                    alpha_quality,
                     method,
                 )
 
@@ -310,7 +312,7 @@ def _save_all(im, fp, filename):
         im.seek(cur_idx)
 
     # Force encoder to flush frames
-    enc.add(None, round(timestamp), 0, 0, "", lossless, quality, 0)
+    enc.add(None, round(timestamp), 0, 0, "", lossless, quality, alpha_quality, 0)
 
     # Get the final output from the encoder
     data = enc.assemble(icc_profile, exif, xmp)
@@ -324,6 +326,7 @@ def _save_all(im, fp, filename):
 def _save(im, fp, filename):
     lossless = im.encoderinfo.get("lossless", False)
     quality = im.encoderinfo.get("quality", 80)
+    alpha_quality = im.encoderinfo.get("alpha_quality", 100)
     icc_profile = im.encoderinfo.get("icc_profile") or ""
     exif = im.encoderinfo.get("exif", b"")
     if isinstance(exif, Image.Exif):
@@ -343,6 +346,7 @@ def _save(im, fp, filename):
         im.size[1],
         lossless,
         float(quality),
+        float(alpha_quality),
         im.mode,
         icc_profile,
         method,


Test patch:
diff --git a/Tests/test_file_webp_alpha.py b/Tests/test_file_webp_alpha.py
--- a/Tests/test_file_webp_alpha.py
+++ b/Tests/test_file_webp_alpha.py
@@ -151,3 +151,15 @@ def test_write_unsupported_mode_PA(tmp_path: Path) -> None:
             target = im.convert("RGBA")
 
         assert_image_similar(image, target, 25.0)
+
+
+def test_alpha_quality(tmp_path: Path) -> None:
+    with Image.open("Tests/images/transparent.png") as im:
+        out = str(tmp_path / "temp.webp")
+        im.save(out)
+
+        out_quality = str(tmp_path / "quality.webp")
+        im.save(out_quality, alpha_quality=50)
+        with Image.open(out) as reloaded:
+            with Image.open(out_quality) as reloaded_quality:
+                assert reloaded.tobytes() != reloaded_quality.tobytes()
diff --git a/Tests/test_file_webp_animated.py b/Tests/test_file_webp_animated.py
--- a/Tests/test_file_webp_animated.py
+++ b/Tests/test_file_webp_animated.py
@@ -188,3 +188,21 @@ def test_seek_errors() -> None:
 
         with pytest.raises(EOFError):
             im.seek(42)
+
+
+def test_alpha_quality(tmp_path: Path) -> None:
+    with Image.open("Tests/images/transparent.png") as im:
+        first_frame = Image.new("L", im.size)
+
+        out = str(tmp_path / "temp.webp")
+        first_frame.save(out, save_all=True, append_images=[im])
+
+        out_quality = str(tmp_path / "quality.webp")
+        first_frame.save(
+            out_quality, save_all=True, append_images=[im], alpha_quality=50
+        )
+        with Image.open(out) as reloaded:
+            reloaded.seek(1)
+            with Image.open(out_quality) as reloaded_quality:
+                reloaded_quality.seek(1)
+                assert reloaded.tobytes() != reloaded_quality.tobytes()