Vicy_Publique/utils/image_generator.py
2026-08-16 19:37:15 +02:00

50 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
from PIL import Image, ImageDraw, ImageFont
import os
def generate_bank_image(user_id, balance, transactions):
"""Génère une image avec les informations bancaires de l'utilisateur."""
image_path = "ressources/assets/template.png"
if not os.path.exists(image_path):
print(f"❌ Erreur : Fichier {image_path} introuvable.")
return None
image = Image.open(image_path)
draw = ImageDraw.Draw(image)
# Vérifier que la police existe
font_path = "ressources/font.ttf"
if not os.path.exists(font_path):
print("❌ Erreur : Police introuvable, chargement de la police par défaut.")
font_large = ImageFont.load_default()
font_small = ImageFont.load_default()
else:
font_large = ImageFont.truetype(font_path, 120)
font_small = ImageFont.truetype(font_path, 30)
# Centrer le solde en dessous de la fenêtre bleue
balance_text = f"Solde : {balance} €"
balance_x = (image.width - draw.textlength(balance_text, font=font_large)) // 2
balance_y = 850 # Ajustez cette valeur pour positionner le texte en dessous de la fenêtre bleue
draw.text((balance_x, balance_y), balance_text, fill="white", font=font_large)
# Affichage des transactions
start_x = 50 # Ajustez cette valeur pour l'alignement horizontal
start_y = 400 # Ajustez cette valeur pour l'alignement vertical
spacing_y = 60 # Espace entre les lignes de transaction
for transaction in transactions[:3]: # Max 3 transactions
transaction_text = f"{transaction['date']} - {transaction['type']} : {transaction['montant']}€"
draw.text((start_x, start_y), transaction_text, fill="black", font=font_small)
start_y += spacing_y
# Supprimer le texte "Victoria’s Vice™"
erase_box = (30, image.height - 50, 250, image.height - 20)
draw.rectangle(erase_box, fill=(255, 255, 255))
# Sauvegarde de l'image générée
output_path = f"ressources/bank_{user_id}.png"
image.save(output_path)
print(f"✅ Image bancaire générée : {output_path}")
return output_path