Update app.py
Browse files
app.py
CHANGED
|
@@ -155,12 +155,29 @@ def _compress_image(raw_bytes: bytes, quality: int, fmt: str) -> bytes:
|
|
| 155 |
method=4, # 0-6, higher = slower + better compression
|
| 156 |
)
|
| 157 |
elif fmt == "avif":
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
img.save(
|
| 161 |
out_buffer,
|
| 162 |
format="AVIF",
|
| 163 |
quality=quality,
|
|
|
|
| 164 |
)
|
| 165 |
elif fmt == "heif":
|
| 166 |
if not HEIF_AVAILABLE:
|
|
|
|
| 155 |
method=4, # 0-6, higher = slower + better compression
|
| 156 |
)
|
| 157 |
elif fmt == "avif":
|
| 158 |
+
# NOTE: Pillow 12.x has NATIVE AVIF support via AvifImagePlugin.
|
| 159 |
+
# It does NOT use pillow-heif's 'chroma' parameter.
|
| 160 |
+
# The correct parameter is 'subsampling' (default '4:2:0').
|
| 161 |
+
# We MUST use '4:4:4' to prevent alpha edge bleeding on transparent images.
|
| 162 |
+
|
| 163 |
+
if img.mode in ("RGBA", "LA", "PA") or "transparency" in img.info:
|
| 164 |
+
img = img.convert("RGBA")
|
| 165 |
+
import numpy as np
|
| 166 |
+
data = np.array(img)
|
| 167 |
+
alpha = data[:, :, 3]
|
| 168 |
+
|
| 169 |
+
# Remove hidden RGB in fully transparent pixels
|
| 170 |
+
data[:, :, :3][alpha == 0] = [0, 0, 0]
|
| 171 |
+
|
| 172 |
+
img = Image.fromarray(data, "RGBA")
|
| 173 |
+
elif img.mode not in ("RGB", "L"):
|
| 174 |
+
img = img.convert("RGB")
|
| 175 |
+
|
| 176 |
img.save(
|
| 177 |
out_buffer,
|
| 178 |
format="AVIF",
|
| 179 |
quality=quality,
|
| 180 |
+
subsampling="4:4:4",
|
| 181 |
)
|
| 182 |
elif fmt == "heif":
|
| 183 |
if not HEIF_AVAILABLE:
|