llm_ticket3/utils/image_preparer.py
2025-05-06 16:48:43 +02:00

24 lines
773 B
Python

from PIL import Image, ImageOps
from pathlib import Path
BICUBIC = Image.Resampling.BICUBIC # Nouvelle façon d'accéder à BICUBIC
def prepare_image_for_llama_vision(input_path: str, output_path: str, size=(672, 672)) -> str:
from PIL import ImageOps, Image
img = Image.open(input_path)
if img.mode != "RGB":
img = img.convert("RGB")
# Redimensionne en conservant le ratio
img.thumbnail(size, Image.Resampling.BICUBIC)
# Ajoute du padding pour obtenir exactement 672x672
padded_img = Image.new("RGB", size, (255, 255, 255)) # fond blanc
offset = ((size[0] - img.width) // 2, (size[1] - img.height) // 2)
padded_img.paste(img, offset)
padded_img.save(output_path, format="PNG", optimize=True)
return output_path