Files
photobooth/backend/collage.py
Jules c8e4ec80c1 Init photobooth - borne photo evenementielle RPi4
Backend FastAPI complet : camera gphoto2, filtres/overlays Pillow,
chroma key OpenCV, multi-shot/collage, GIF, impression CUPS, email SMTP,
QR code. Frontend web vanilla (HTML/CSS/JS) pour Chromium kiosk.
Menu admin cache (appui long coin ecran). Toutes les fonctionnalites
activables/desactivables. Scripts install + systemd pour RPi4.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-21 21:46:31 +01:00

72 lines
2.4 KiB
Python

import logging
from pathlib import Path
from PIL import Image, ImageDraw
from backend.config import DOSSIER_EXPORTS, charger_config
log = logging.getLogger("photobooth.collage")
def creer_strip(photos: list[Path], marge: int = 20, couleur_fond: str = "#ffffff") -> Path:
"""Cree une bande verticale (strip) avec les photos."""
images = [Image.open(p) for p in photos]
# Uniformiser la largeur
largeur = min(img.width for img in images)
images_redim = []
for img in images:
ratio = largeur / img.width
hauteur = int(img.height * ratio)
images_redim.append(img.resize((largeur, hauteur), Image.LANCZOS))
hauteur_totale = sum(img.height for img in images_redim) + marge * (len(images_redim) + 1)
largeur_totale = largeur + marge * 2
strip = Image.new("RGB", (largeur_totale, hauteur_totale), couleur_fond)
y = marge
for img in images_redim:
strip.paste(img, (marge, y))
y += img.height + marge
nom = f"strip_{photos[0].stem}.jpg"
chemin = DOSSIER_EXPORTS / nom
strip.save(chemin, "JPEG", quality=95)
log.info(f"Strip cree : {chemin}")
return chemin
def creer_collage(photos: list[Path], colonnes: int = 2, marge: int = 15,
couleur_fond: str = "#ffffff") -> Path:
"""Cree un collage en grille avec les photos."""
images = [Image.open(p) for p in photos]
# Taille uniforme pour chaque cellule
largeur_cellule = min(img.width for img in images)
hauteur_cellule = min(img.height for img in images)
images_redim = []
for img in images:
img_crop = ImageDraw.Draw(img)
img_redim = img.resize((largeur_cellule, hauteur_cellule), Image.LANCZOS)
images_redim.append(img_redim)
lignes = (len(images_redim) + colonnes - 1) // colonnes
largeur_totale = colonnes * largeur_cellule + (colonnes + 1) * marge
hauteur_totale = lignes * hauteur_cellule + (lignes + 1) * marge
collage = Image.new("RGB", (largeur_totale, hauteur_totale), couleur_fond)
for i, img in enumerate(images_redim):
col = i % colonnes
lig = i // colonnes
x = marge + col * (largeur_cellule + marge)
y = marge + lig * (hauteur_cellule + marge)
collage.paste(img, (x, y))
nom = f"collage_{photos[0].stem}.jpg"
chemin = DOSSIER_EXPORTS / nom
collage.save(chemin, "JPEG", quality=95)
log.info(f"Collage cree : {chemin}")
return chemin