Fix 16:9 output + MIME detection for Image-to-Image
- Pillow installiert (fehlte → try/except lief ins Leere) - _detect_image_mime(): Magic-Bytes-Erkennung (JPEG/PNG/WebP/HEIC) - _gemini_generate_image(): kein hartes 'image/jpeg' mehr - PIL-Resize 1920×1080 greift jetzt korrekt
This commit is contained in:
+21
-1
@@ -87,6 +87,21 @@ def _get_gemini_key() -> str:
|
|||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def _detect_image_mime(image_bytes: bytes) -> str:
|
||||||
|
"""MIME-Type anhand Magic-Bytes erkennen."""
|
||||||
|
if len(image_bytes) < 8:
|
||||||
|
return "image/jpeg" # Fallback
|
||||||
|
if image_bytes[:4] == b"\x89PNG":
|
||||||
|
return "image/png"
|
||||||
|
if image_bytes[:2] == b"\xff\xd8":
|
||||||
|
return "image/jpeg"
|
||||||
|
if image_bytes[:4] == b"RIFF" and image_bytes[8:12] == b"WEBP":
|
||||||
|
return "image/webp"
|
||||||
|
if image_bytes[:4] in (b"ftyp", b"heic", b"heix", b"hevc", b"hevx"):
|
||||||
|
return "image/heic"
|
||||||
|
return "image/jpeg" # Fallback
|
||||||
|
|
||||||
|
|
||||||
def _gemini_generate_image(
|
def _gemini_generate_image(
|
||||||
prompt: str,
|
prompt: str,
|
||||||
image_base64: str | None,
|
image_base64: str | None,
|
||||||
@@ -104,15 +119,20 @@ def _gemini_generate_image(
|
|||||||
Returns:
|
Returns:
|
||||||
dict mit "image_base64" (PNG) oder "error".
|
dict mit "image_base64" (PNG) oder "error".
|
||||||
"""
|
"""
|
||||||
|
import base64 as b64
|
||||||
|
|
||||||
key = _get_gemini_key()
|
key = _get_gemini_key()
|
||||||
if not key:
|
if not key:
|
||||||
return {"error": "Kein Gemini API-Key"}
|
return {"error": "Kein Gemini API-Key"}
|
||||||
|
|
||||||
parts = [{"text": f"{prompt}\n\nAspect ratio: {aspect_ratio}."}]
|
parts = [{"text": f"{prompt}\n\nAspect ratio: {aspect_ratio}."}]
|
||||||
if image_base64:
|
if image_base64:
|
||||||
|
# MIME-Type aus den tatsächlichen Bilddaten erkennen
|
||||||
|
raw_bytes = b64.b64decode(image_base64)
|
||||||
|
mime_type = _detect_image_mime(raw_bytes)
|
||||||
parts.append({
|
parts.append({
|
||||||
"inlineData": {
|
"inlineData": {
|
||||||
"mimeType": "image/jpeg",
|
"mimeType": mime_type,
|
||||||
"data": image_base64
|
"data": image_base64
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user