mirror of
https://github.com/Ladebeze66/llm_ticket3.git
synced 2025-12-13 12:36:50 +01:00
33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from PIL import Image, ImageOps
|
|
from pathlib import Path
|
|
import os
|
|
from typing import Optional
|
|
|
|
BICUBIC = Image.Resampling.BICUBIC # Nouvelle façon d'accéder à BICUBIC
|
|
|
|
def prepare_image_for_llama_vision(input_path: str, output_path: Optional[str] = None, size=(672, 672)) -> str:
|
|
"""Prépare une image pour être utilisée avec le modèle Llama Vision en la redimensionnant et en ajoutant du padding."""
|
|
from PIL import ImageOps, Image
|
|
|
|
# Si aucun chemin de sortie n'est spécifié, utiliser le répertoire par défaut
|
|
if output_path is None:
|
|
os.makedirs("results/ocr_avance", exist_ok=True)
|
|
image_name = Path(input_path).stem
|
|
output_path = f"results/ocr_avance/vision_ready_{image_name}.png"
|
|
|
|
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
|
|
|