fix: normalize animated images before gemini
This commit is contained in:
@@ -110,11 +110,18 @@ class AutoImagePipelineService:
|
||||
@staticmethod
|
||||
def _normalize_image_for_gemini(image_path: Path) -> Path:
|
||||
suffix = image_path.suffix.lower()
|
||||
if suffix not in {".avif", ".webp"}:
|
||||
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")
|
||||
with Image.open(image_path) as img:
|
||||
if is_animated:
|
||||
try:
|
||||
img.seek(0)
|
||||
except Exception:
|
||||
pass
|
||||
if img.mode not in ("RGB", "L"):
|
||||
img = img.convert("RGB")
|
||||
img.save(normalized_path, format="JPEG", quality=95)
|
||||
|
||||
Reference in New Issue
Block a user