fix: normalize animated images before gemini

This commit is contained in:
2026-03-09 14:57:41 +08:00
parent a2119f3b6d
commit 5fcce98583

View File

@@ -110,11 +110,18 @@ class AutoImagePipelineService:
@staticmethod @staticmethod
def _normalize_image_for_gemini(image_path: Path) -> Path: def _normalize_image_for_gemini(image_path: Path) -> Path:
suffix = image_path.suffix.lower() suffix = image_path.suffix.lower()
if suffix not in {".avif", ".webp"}:
return image_path
normalized_path = image_path.with_suffix(".jpg")
with Image.open(image_path) as img: with Image.open(image_path) as img:
is_animated = bool(getattr(img, "is_animated", False)) or int(getattr(img, "n_frames", 1) or 1) > 1
needs_convert = suffix in {".avif", ".webp", ".gif"} or is_animated or img.mode not in ("RGB", "L")
if not needs_convert:
return image_path
normalized_path = image_path.with_suffix(".jpg")
if is_animated:
try:
img.seek(0)
except Exception:
pass
if img.mode not in ("RGB", "L"): if img.mode not in ("RGB", "L"):
img = img.convert("RGB") img = img.convert("RGB")
img.save(normalized_path, format="JPEG", quality=95) img.save(normalized_path, format="JPEG", quality=95)