137 lines
4.2 KiB
Python
137 lines
4.2 KiB
Python
import pygraphviz as pgv
|
|
import json
|
|
import os
|
|
from PIL import Image, ImageDraw, ImageFont, ImageOps
|
|
|
|
def create_avatar_with_name(user_id, name, avatar_path, output_path):
|
|
"""Crée une image combinant avatar rond et pseudo en dessous"""
|
|
try:
|
|
avatar = Image.open(avatar_path).convert("RGBA")
|
|
avatar = avatar.resize((150, 150))
|
|
|
|
mask = Image.new('L', (150, 150), 0)
|
|
draw = ImageDraw.Draw(mask)
|
|
draw.ellipse((0, 0, 150, 150), fill=255)
|
|
rounded_avatar = ImageOps.fit(avatar, mask.size, centering=(0.5, 0.5))
|
|
rounded_avatar.putalpha(mask)
|
|
|
|
combined = Image.new('RGBA', (200, 200), (0, 0, 0, 0))
|
|
combined.paste(rounded_avatar, (25, 0), rounded_avatar)
|
|
|
|
draw = ImageDraw.Draw(combined)
|
|
try:
|
|
font = ImageFont.truetype("arial.ttf", 14)
|
|
except:
|
|
font = ImageFont.load_default()
|
|
|
|
text_width = draw.textlength(name, font=font)
|
|
draw.text(((200 - text_width) / 2, 160), name, font=font, fill="white")
|
|
|
|
combined.save(output_path)
|
|
return True
|
|
except Exception as e:
|
|
print(f"Erreur création avatar+texte {user_id}: {e}")
|
|
return False
|
|
|
|
def generate_tree(input_file, output_file, root_id=None):
|
|
with open(input_file) as f:
|
|
data = json.load(f)
|
|
|
|
G = pgv.AGraph(
|
|
directed=True,
|
|
rankdir="TB",
|
|
nodesep="0.8",
|
|
ranksep="1.5",
|
|
bgcolor="#000000",
|
|
splines="true"
|
|
)
|
|
|
|
node_style = {
|
|
"shape": "none",
|
|
"imagescale": "false",
|
|
"labelloc": "b",
|
|
"width": "2",
|
|
"height": "2",
|
|
"fixedsize": "true"
|
|
}
|
|
|
|
edge_style = {
|
|
"color": "#FFFFFF",
|
|
"penwidth": "3",
|
|
"arrowsize": "1.2"
|
|
}
|
|
|
|
couple_style = {
|
|
"style": "dashed",
|
|
"color": "#FF69B4",
|
|
"penwidth": "2.5",
|
|
"dir": "none"
|
|
}
|
|
|
|
os.makedirs("temp_avatars", exist_ok=True)
|
|
|
|
# 1. Création de tous les nœuds
|
|
for user_id, info in data["members"].items():
|
|
original_avatar = f"avatars/{user_id}.png"
|
|
combined_avatar = f"temp_avatars/combined_{user_id}.png"
|
|
|
|
if os.path.exists(original_avatar):
|
|
create_avatar_with_name(user_id, info["name"], original_avatar, combined_avatar)
|
|
G.add_node(user_id, image=combined_avatar, **node_style)
|
|
else:
|
|
G.add_node(user_id, label=info["name"], **{**node_style, "fontcolor": "white"})
|
|
|
|
# 2. Identification des niveaux
|
|
top_level = set()
|
|
|
|
# Ajout des racines explicites
|
|
if root_id:
|
|
top_level.add(root_id)
|
|
else:
|
|
for root in data.get("roots", []):
|
|
top_level.add(root)
|
|
|
|
# Ajout des couples sans parents
|
|
for couple in data.get("couples", []):
|
|
if all(m in data["members"] and not data["members"][m].get("parents") for m in couple):
|
|
top_level.update(couple)
|
|
|
|
# 3. Création des sous-graphes hiérarchiques
|
|
if top_level:
|
|
G.add_subgraph(top_level, name="rank_top", rank="same")
|
|
|
|
# Niveau des enfants directs
|
|
children_level = set()
|
|
for user_id, info in data["members"].items():
|
|
parents = info.get("parents", [])
|
|
if any(p["id"] in top_level for p in parents):
|
|
children_level.add(user_id)
|
|
|
|
if children_level:
|
|
G.add_subgraph(children_level, name="rank_children", rank="same")
|
|
|
|
# 4. Gestion des couples
|
|
for couple in data.get("couples", []):
|
|
if all(m in data["members"] for m in couple):
|
|
member1, member2 = couple
|
|
|
|
# Lien conjugal
|
|
G.add_edge(member1, member2, **couple_style)
|
|
|
|
# Alignement forcé pour les couples racines
|
|
if member1 in top_level and member2 in top_level:
|
|
G.add_subgraph(couple, name=f"couple_{member1}_{member2}", rank="same")
|
|
|
|
# 5. Liens parent-enfant
|
|
for user_id, info in data["members"].items():
|
|
for parent in info.get("parents", []):
|
|
if parent["id"] in data["members"]:
|
|
G.add_edge(parent["id"], user_id, **edge_style)
|
|
|
|
G.layout(prog="dot")
|
|
G.draw(output_file)
|
|
|
|
# Nettoyage
|
|
for f in os.listdir("temp_avatars"):
|
|
os.remove(f"temp_avatars/{f}")
|