diff --git a/services/service_auto_image_pipeline.py b/services/service_auto_image_pipeline.py index e1aa7e5..d131301 100644 --- a/services/service_auto_image_pipeline.py +++ b/services/service_auto_image_pipeline.py @@ -11,6 +11,7 @@ from urllib.parse import urlparse import httpx from dotenv import load_dotenv +from PIL import Image from db.customer_db import CustomerDatabase from db.image_tasks_db import TaskStatus, db as task_db @@ -90,6 +91,22 @@ class AutoImagePipelineService: suffix = suffix or dest_path.suffix or ".bin" return dest_path.with_suffix(suffix) + @staticmethod + def _normalize_image_for_gemini(image_path: Path) -> Path: + 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: + if img.mode not in ("RGB", "L"): + img = img.convert("RGB") + img.save(normalized_path, format="JPEG", quality=95) + logger.info( + f"[AutoImagePipeline] 已转换图片格式供Gemini使用: src={image_path} normalized={normalized_path}" + ) + return normalized_path + async def _download_image(self, image_url: str, dest_path: Path) -> Path: dest_path.parent.mkdir(parents=True, exist_ok=True) timeout = httpx.Timeout(60.0, connect=20.0) @@ -239,6 +256,7 @@ class AutoImagePipelineService: ) try: input_path = await self._download_image(image_url, input_path) + input_path = self._normalize_image_for_gemini(input_path) success, message, data = await gemini_service.extract_pattern( str(input_path), str(output_path),