fix: skip aspect_ratio for kling-o1 models and detect image MIME via Pillow
kling-o1-pro and kling-o1-std silently fail when aspect_ratio is included in the payload — they derive it from the input image. Added VIDEO_ASPECT_RATIO_MODELS whitelist so only kling-elements and minimax-hailuo receive the parameter. Also switched image_to_base64 to use Pillow for format detection instead of trusting the file extension, which correctly handles files saved with the wrong extension. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -20,16 +20,24 @@ from rich.progress import (
|
||||
|
||||
|
||||
def image_to_base64(path: Path) -> str:
|
||||
"""Read an image file and return a base64-encoded string."""
|
||||
suffix = path.suffix.lower().lstrip(".")
|
||||
mime_map = {
|
||||
"jpg": "image/jpeg",
|
||||
"jpeg": "image/jpeg",
|
||||
"png": "image/png",
|
||||
"gif": "image/gif",
|
||||
"webp": "image/webp",
|
||||
"""Read an image file and return a base64-encoded string.
|
||||
|
||||
Uses Pillow to detect the actual format rather than trusting the file
|
||||
extension — mismatched extensions (e.g. a JPEG saved as .png) would
|
||||
produce an incorrect MIME type that causes silent failures with some models.
|
||||
"""
|
||||
from PIL import Image
|
||||
|
||||
_pillow_to_mime = {
|
||||
"JPEG": "image/jpeg",
|
||||
"PNG": "image/png",
|
||||
"GIF": "image/gif",
|
||||
"WEBP": "image/webp",
|
||||
}
|
||||
mime = mime_map.get(suffix, "image/jpeg")
|
||||
with Image.open(path) as img:
|
||||
fmt = img.format or "JPEG"
|
||||
mime = _pillow_to_mime.get(fmt, "image/jpeg")
|
||||
|
||||
with open(path, "rb") as f:
|
||||
encoded = base64.b64encode(f.read()).decode()
|
||||
return f"data:{mime};base64,{encoded}"
|
||||
|
||||
Reference in New Issue
Block a user