You've already forked etude_lego_jurassic_world
Corrige l'alias Franklin Webb et régénère les frises
This commit is contained in:
115
lib/plots/minifig_character_collages.py
Normal file
115
lib/plots/minifig_character_collages.py
Normal file
@@ -0,0 +1,115 @@
|
||||
"""Génère des frises horizontales de minifigs par personnage."""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Dict, Iterable, List, Sequence, Set
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
from lib.filesystem import ensure_parent_dir
|
||||
from lib.rebrickable.resources import sanitize_name
|
||||
|
||||
|
||||
def resize_to_height(image: Image.Image, target_height: int) -> Image.Image:
|
||||
"""Redimensionne une image en conservant le ratio selon une hauteur cible."""
|
||||
width, height = image.size
|
||||
ratio = target_height / height
|
||||
new_width = int(width * ratio)
|
||||
return image.resize((new_width, target_height), Image.Resampling.LANCZOS)
|
||||
|
||||
|
||||
def render_label(width: int, height: int, text: str, font: ImageFont.ImageFont) -> Image.Image:
|
||||
"""Construit l'étiquette textuelle centrée sous une minifig."""
|
||||
label = Image.new("RGB", (width, height), (255, 255, 255))
|
||||
drawer = ImageDraw.Draw(label)
|
||||
bbox = drawer.textbbox((0, 0), text, font=font)
|
||||
text_width = bbox[2] - bbox[0]
|
||||
text_height = bbox[3] - bbox[1]
|
||||
x = (width - text_width) // 2
|
||||
y = (height - text_height) // 2
|
||||
drawer.text((x, y), text, fill=(0, 0, 0), font=font)
|
||||
return label
|
||||
|
||||
|
||||
def build_placeholder(image_height: int) -> Image.Image:
|
||||
"""Crée un rectangle neutre pour signaler une image manquante."""
|
||||
placeholder = Image.new("RGBA", (image_height, image_height), (220, 220, 220, 255))
|
||||
drawer = ImageDraw.Draw(placeholder)
|
||||
drawer.rectangle([(0, 0), (image_height - 1, image_height - 1)], outline=(150, 150, 150), width=2)
|
||||
drawer.line([(0, 0), (image_height - 1, image_height - 1)], fill=(200, 80, 80), width=3)
|
||||
drawer.line([(image_height - 1, 0), (0, image_height - 1)], fill=(200, 80, 80), width=3)
|
||||
return placeholder
|
||||
|
||||
|
||||
def build_cell(image: Image.Image, label: str, image_height: int, label_height: int, font: ImageFont.ImageFont) -> Image.Image:
|
||||
"""Assemble une minifig redimensionnée et son étiquette associée."""
|
||||
resized = resize_to_height(image, image_height)
|
||||
label_img = render_label(resized.width, label_height, label, font).convert("RGBA")
|
||||
cell = Image.new("RGBA", (resized.width, resized.height + label_height), (255, 255, 255, 255))
|
||||
cell.paste(resized, (0, 0))
|
||||
cell.paste(label_img, (0, resized.height))
|
||||
return cell
|
||||
|
||||
|
||||
def build_character_collage(
|
||||
character: str,
|
||||
entries: Sequence[dict],
|
||||
resources_dir: Path,
|
||||
destination_dir: Path,
|
||||
font: ImageFont.ImageFont,
|
||||
missing_paths: Set[str] | None = None,
|
||||
image_height: int = 260,
|
||||
label_height: int = 44,
|
||||
spacing: int = 28,
|
||||
) -> Path:
|
||||
"""Construit la frise d'un personnage et la sauvegarde dans le répertoire cible."""
|
||||
sanitized = sanitize_name(character)
|
||||
missing = missing_paths or set()
|
||||
cells: List[Image.Image] = []
|
||||
for row in entries:
|
||||
image_path = resources_dir / row["set_id"] / sanitized / "minifig.jpg"
|
||||
label = f"{row['year']} - {row['set_num']}"
|
||||
if str(image_path) in missing:
|
||||
image = build_placeholder(image_height)
|
||||
else:
|
||||
image = Image.open(image_path).convert("RGBA")
|
||||
cells.append(build_cell(image, label, image_height, label_height, font))
|
||||
total_width = sum(cell.width for cell in cells) + spacing * (len(cells) - 1)
|
||||
total_height = max(cell.height for cell in cells)
|
||||
canvas = Image.new("RGB", (total_width, total_height), (255, 255, 255))
|
||||
x = 0
|
||||
for cell in cells:
|
||||
canvas.paste(cell.convert("RGB"), (x, 0))
|
||||
x += cell.width + spacing
|
||||
destination_path = destination_dir / f"{sanitized}.png"
|
||||
ensure_parent_dir(destination_path)
|
||||
canvas.save(destination_path)
|
||||
return destination_path
|
||||
|
||||
|
||||
def build_character_collages(
|
||||
grouped_entries: Dict[str, Sequence[dict]],
|
||||
resources_dir: Path,
|
||||
destination_dir: Path,
|
||||
missing_paths: Set[str] | None = None,
|
||||
image_height: int = 260,
|
||||
label_height: int = 44,
|
||||
spacing: int = 28,
|
||||
) -> List[Path]:
|
||||
"""Construit les frises pour chaque personnage."""
|
||||
font = ImageFont.load_default()
|
||||
generated: List[Path] = []
|
||||
for character, entries in grouped_entries.items():
|
||||
generated.append(
|
||||
build_character_collage(
|
||||
character,
|
||||
entries,
|
||||
resources_dir,
|
||||
destination_dir,
|
||||
font,
|
||||
missing_paths=missing_paths,
|
||||
image_height=image_height,
|
||||
label_height=label_height,
|
||||
spacing=spacing,
|
||||
)
|
||||
)
|
||||
return generated
|
||||
95
lib/rebrickable/minifig_character_sets.py
Normal file
95
lib/rebrickable/minifig_character_sets.py
Normal file
@@ -0,0 +1,95 @@
|
||||
"""Liste les sets contenant chaque personnage connu."""
|
||||
|
||||
import csv
|
||||
from pathlib import Path
|
||||
from typing import Dict, Iterable, List, Sequence
|
||||
|
||||
from lib.filesystem import ensure_parent_dir
|
||||
from lib.rebrickable.stats import read_rows
|
||||
|
||||
|
||||
def load_minifigs_by_set(path: Path) -> List[dict]:
|
||||
"""Charge le CSV minifigs_by_set."""
|
||||
return read_rows(path)
|
||||
|
||||
|
||||
def load_sets(path: Path) -> Dict[str, dict]:
|
||||
"""Indexe les sets enrichis par set_num."""
|
||||
lookup: Dict[str, dict] = {}
|
||||
for row in read_rows(path):
|
||||
lookup[row["set_num"]] = row
|
||||
return lookup
|
||||
|
||||
|
||||
def build_character_sets(
|
||||
minifigs_rows: Iterable[dict],
|
||||
sets_lookup: Dict[str, dict],
|
||||
excluded_characters: Sequence[str] | None = None,
|
||||
) -> List[dict]:
|
||||
"""Associe chaque personnage connu aux sets où il apparaît (hors figurants)."""
|
||||
excluded = set(excluded_characters or [])
|
||||
seen: set[tuple[str, str, str]] = set()
|
||||
character_sets: List[dict] = []
|
||||
for row in minifigs_rows:
|
||||
character = row["known_character"].strip()
|
||||
fig_num = row["fig_num"].strip()
|
||||
if character == "" or fig_num == "":
|
||||
continue
|
||||
if character in excluded:
|
||||
continue
|
||||
set_row = sets_lookup[row["set_num"]]
|
||||
key = (character, row["set_num"], fig_num)
|
||||
if key in seen:
|
||||
continue
|
||||
character_sets.append(
|
||||
{
|
||||
"known_character": character,
|
||||
"set_num": row["set_num"],
|
||||
"set_id": set_row["set_id"],
|
||||
"year": set_row["year"],
|
||||
"fig_num": fig_num,
|
||||
}
|
||||
)
|
||||
seen.add(key)
|
||||
character_sets.sort(key=lambda row: (row["known_character"], int(row["year"]), row["set_num"], row["fig_num"]))
|
||||
return character_sets
|
||||
|
||||
|
||||
def write_character_sets(destination_path: Path, rows: Sequence[dict]) -> None:
|
||||
"""Écrit le CSV listant les sets par personnage."""
|
||||
ensure_parent_dir(destination_path)
|
||||
fieldnames = ["known_character", "set_num", "set_id", "year", "fig_num"]
|
||||
with destination_path.open("w", newline="") as csv_file:
|
||||
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
|
||||
writer.writeheader()
|
||||
for row in rows:
|
||||
writer.writerow(row)
|
||||
|
||||
|
||||
def group_by_character(rows: Iterable[dict]) -> Dict[str, List[dict]]:
|
||||
"""Regroupe les lignes par personnage pour la génération des frises."""
|
||||
grouped: Dict[str, List[dict]] = {}
|
||||
for row in rows:
|
||||
character = row["known_character"]
|
||||
entries = grouped.get(character)
|
||||
if entries is None:
|
||||
entries = []
|
||||
grouped[character] = entries
|
||||
entries.append(row)
|
||||
for character, entries in grouped.items():
|
||||
entries.sort(key=lambda row: (int(row["year"]), row["set_num"], row["fig_num"]))
|
||||
return grouped
|
||||
|
||||
|
||||
def load_missing_minifigs(path: Path) -> set[str]:
|
||||
"""Charge les ressources minifigs introuvables consignées dans le log de téléchargement."""
|
||||
missing: set[str] = set()
|
||||
with path.open() as csv_file:
|
||||
reader = csv.DictReader(csv_file)
|
||||
for row in reader:
|
||||
if row["status"] != "missing":
|
||||
continue
|
||||
if not row["path"].endswith("minifig.jpg"):
|
||||
continue
|
||||
missing.add(row["path"])
|
||||
return missing
|
||||
Reference in New Issue
Block a user