Normalize image to JPEG before vision analysis (fixes HEIF/HEIC support)

analyze_user_image now runs input bytes through Pillow before base64-
encoding, converting HEIC/HEIF and any other non-JPEG format to JPEG.
pillow_heif is already registered at module load so HEIC opens cleanly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-12 14:12:26 -04:00
parent 684460903f
commit 600791ea5d
+13
View File
@@ -1,5 +1,6 @@
from ollama import Client
import argparse
import io
import os
import logging
from PIL import Image, ExifTags
@@ -76,6 +77,16 @@ def describe_simba_image(input):
return result
def _to_jpeg_bytes(file_bytes: bytes) -> bytes:
"""Normalize image bytes to JPEG, handling HEIC/HEIF and other formats."""
img = Image.open(io.BytesIO(file_bytes))
if img.mode not in ("RGB", "L"):
img = img.convert("RGB")
buf = io.BytesIO()
img.save(buf, format="JPEG", quality=85)
return buf.getvalue()
async def analyze_user_image(file_bytes: bytes) -> str:
"""Analyze an image uploaded by a user and return a text description.
@@ -86,6 +97,8 @@ async def analyze_user_image(file_bytes: bytes) -> str:
from openai import AsyncOpenAI
file_bytes = _to_jpeg_bytes(file_bytes)
llama_url = os.getenv("LLAMA_SERVER_URL")
if llama_url:
aclient = AsyncOpenAI(base_url=llama_url, api_key="not-needed")