Spaces:
Sleeping
Sleeping
Delete src/graphics.py
Browse files- src/graphics.py +0 -146
src/graphics.py
DELETED
|
@@ -1,146 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import uuid
|
| 3 |
-
from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageOps
|
| 4 |
-
from .config import TMP_DIR, W, H, THEMES, FONT_REG, FONT_BOLD
|
| 5 |
-
|
| 6 |
-
# ----------------------------------------------------------------------
|
| 7 |
-
# 🧩 Fonctions utilitaires
|
| 8 |
-
# ----------------------------------------------------------------------
|
| 9 |
-
|
| 10 |
-
def safe_name(base):
|
| 11 |
-
"""Crée un nom de fichier sûr sans caractères spéciaux."""
|
| 12 |
-
import re
|
| 13 |
-
return re.sub(r'[^a-zA-Z0-9_-]+', '_', str(base)).strip('_')
|
| 14 |
-
|
| 15 |
-
def _wrap_text(text, font, max_width, draw):
|
| 16 |
-
"""Découpe le texte pour qu’il tienne dans la largeur donnée."""
|
| 17 |
-
words = text.split()
|
| 18 |
-
lines = []
|
| 19 |
-
line = ""
|
| 20 |
-
|
| 21 |
-
for word in words:
|
| 22 |
-
test_line = f"{line} {word}".strip()
|
| 23 |
-
if draw.textlength(test_line, font=font) <= max_width:
|
| 24 |
-
line = test_line
|
| 25 |
-
else:
|
| 26 |
-
lines.append(line)
|
| 27 |
-
line = word
|
| 28 |
-
if line:
|
| 29 |
-
lines.append(line)
|
| 30 |
-
return "\n".join(lines)
|
| 31 |
-
|
| 32 |
-
def _draw_text_shadow(draw, position, text, font, fill, shadow_color=(0, 0, 0)):
|
| 33 |
-
"""Dessine un texte avec ombre portée pour plus de lisibilité."""
|
| 34 |
-
x, y = position
|
| 35 |
-
shadow_offset = 3
|
| 36 |
-
draw.text((x + shadow_offset, y + shadow_offset), text, font=font, fill=shadow_color)
|
| 37 |
-
draw.text((x, y), text, font=font, fill=fill)
|
| 38 |
-
|
| 39 |
-
# ----------------------------------------------------------------------
|
| 40 |
-
# 🎨 Création du fond d’écran pour capsule
|
| 41 |
-
# ----------------------------------------------------------------------
|
| 42 |
-
|
| 43 |
-
def make_background(
|
| 44 |
-
titre,
|
| 45 |
-
sous_titre,
|
| 46 |
-
texte_ecran,
|
| 47 |
-
theme,
|
| 48 |
-
logo_path=None,
|
| 49 |
-
logo_pos="haut-gauche",
|
| 50 |
-
image_fond=None,
|
| 51 |
-
fond_mode="plein écran"
|
| 52 |
-
):
|
| 53 |
-
"""
|
| 54 |
-
Crée une image de fond personnalisée pour une capsule :
|
| 55 |
-
- couleur et thème
|
| 56 |
-
- logo et position
|
| 57 |
-
- texte à l’écran
|
| 58 |
-
- image de fond optionnelle
|
| 59 |
-
"""
|
| 60 |
-
|
| 61 |
-
print(f"[Fond] Création du fond - Thème: {theme}")
|
| 62 |
-
|
| 63 |
-
try:
|
| 64 |
-
# ---- Définir les couleurs du thème ----
|
| 65 |
-
couleur_1 = tuple(THEMES.get(theme, THEMES["Bleu Professionnel"])["primary"])
|
| 66 |
-
couleur_2 = tuple(THEMES.get(theme, THEMES["Bleu Professionnel"])["secondary"])
|
| 67 |
-
|
| 68 |
-
# ---- Créer un fond uni (ou gradient léger) ----
|
| 69 |
-
bg = Image.new("RGB", (W, H), couleur_1)
|
| 70 |
-
overlay = Image.new("RGB", (W, H), couleur_2)
|
| 71 |
-
bg = Image.blend(bg, overlay, alpha=0.25)
|
| 72 |
-
|
| 73 |
-
draw = ImageDraw.Draw(bg)
|
| 74 |
-
|
| 75 |
-
# ---- Ajouter l’image de fond ----
|
| 76 |
-
if image_fond and os.path.exists(image_fond):
|
| 77 |
-
try:
|
| 78 |
-
img = Image.open(image_fond).convert("RGB")
|
| 79 |
-
if fond_mode == "plein écran":
|
| 80 |
-
img = img.resize((W, H))
|
| 81 |
-
bg.paste(img, (0, 0))
|
| 82 |
-
elif fond_mode == "moitié gauche":
|
| 83 |
-
img = img.resize((W // 2, H))
|
| 84 |
-
bg.paste(img, (0, 0))
|
| 85 |
-
elif fond_mode == "moitié droite":
|
| 86 |
-
img = img.resize((W // 2, H))
|
| 87 |
-
bg.paste(img, (W // 2, 0))
|
| 88 |
-
elif fond_mode == "moitié bas":
|
| 89 |
-
img = img.resize((W, H // 2))
|
| 90 |
-
bg.paste(img, (0, H // 2))
|
| 91 |
-
print(f"[Fond] ✅ Image de fond ajoutée ({fond_mode})")
|
| 92 |
-
except Exception as e:
|
| 93 |
-
print(f"[Fond] ⚠️ Erreur chargement image fond: {e}")
|
| 94 |
-
|
| 95 |
-
# ---- Ajouter le logo ----
|
| 96 |
-
if logo_path and os.path.exists(logo_path):
|
| 97 |
-
try:
|
| 98 |
-
logo = Image.open(logo_path).convert("RGBA")
|
| 99 |
-
max_logo_w = int(W * 0.25)
|
| 100 |
-
ratio = max_logo_w / logo.width
|
| 101 |
-
logo = logo.resize((max_logo_w, int(logo.height * ratio)))
|
| 102 |
-
margin = 50
|
| 103 |
-
|
| 104 |
-
if logo_pos == "haut-gauche":
|
| 105 |
-
pos = (margin, margin)
|
| 106 |
-
elif logo_pos == "haut-droite":
|
| 107 |
-
pos = (W - logo.width - margin, margin)
|
| 108 |
-
elif logo_pos == "centre":
|
| 109 |
-
pos = ((W - logo.width) // 2, (H - logo.height) // 2)
|
| 110 |
-
else:
|
| 111 |
-
pos = (margin, margin)
|
| 112 |
-
|
| 113 |
-
bg.paste(logo, pos, mask=logo)
|
| 114 |
-
print(f"[Fond] ✅ Logo ajouté à la position {logo_pos}")
|
| 115 |
-
except Exception as e:
|
| 116 |
-
print(f"[Fond] ⚠️ Erreur logo: {e}")
|
| 117 |
-
|
| 118 |
-
# ---- Ajouter le texte principal ----
|
| 119 |
-
font_title = ImageFont.truetype(FONT_BOLD, 80)
|
| 120 |
-
font_sub = ImageFont.truetype(FONT_REG, 60)
|
| 121 |
-
font_body = ImageFont.truetype(FONT_REG, 50)
|
| 122 |
-
|
| 123 |
-
margin_x, y_pos = 150, 200
|
| 124 |
-
_draw_text_shadow(draw, (margin_x, y_pos), titre, font_title, fill=(255, 255, 255))
|
| 125 |
-
y_pos += 120
|
| 126 |
-
|
| 127 |
-
if sous_titre:
|
| 128 |
-
_draw_text_shadow(draw, (margin_x, y_pos), sous_titre, font_sub, fill=(255, 255, 255))
|
| 129 |
-
y_pos += 100
|
| 130 |
-
|
| 131 |
-
if texte_ecran:
|
| 132 |
-
max_width = W - 2 * margin_x
|
| 133 |
-
texte_multilignes = _wrap_text(texte_ecran, font_body, max_width, draw)
|
| 134 |
-
_draw_text_shadow(draw, (margin_x, y_pos), texte_multilignes, font_body, fill=(255, 255, 255))
|
| 135 |
-
|
| 136 |
-
# ---- Sauvegarder le fond dans un fichier unique ----
|
| 137 |
-
fond_name = safe_name(titre) if titre else uuid.uuid4().hex[:6]
|
| 138 |
-
out_path = os.path.join(TMP_DIR, f"fond_{fond_name}.png")
|
| 139 |
-
bg.save(out_path)
|
| 140 |
-
print(f"[Fond] ✅ Fond sauvegardé pour la capsule : {out_path}")
|
| 141 |
-
|
| 142 |
-
return out_path
|
| 143 |
-
|
| 144 |
-
except Exception as e:
|
| 145 |
-
print(f"[Fond] ❌ Erreur création du fond : {e}")
|
| 146 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|