1

Collage des pièces imprimées exclusives et variante sans impressions

This commit is contained in:
2025-12-03 17:37:01 +01:00
parent 8560f15b41
commit cebe3046db
6 changed files with 117 additions and 5 deletions

View File

@@ -6,7 +6,7 @@ from typing import List
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnnotationBbox, OffsetImage
from PIL import Image
from PIL import Image, ImageDraw, ImageFont
from lib.filesystem import ensure_parent_dir
@@ -21,6 +21,22 @@ def load_part_rarity(path: Path) -> List[dict]:
return rows
def select_printed_exclusive(rows: List[dict], resources_dir: Path) -> List[dict]:
"""Filtre les pièces imprimées exclusives aux sets filtrés disposant d'une image locale."""
filtered: List[dict] = []
for row in rows:
if row.get("other_sets_quantity", "0") != "0":
continue
if "print" not in row["part_name"].lower():
continue
image_path = resources_dir / row.get("sample_set_id", "") / "rare_parts" / f"{row['part_num']}.jpg"
if not image_path.exists():
continue
filtered.append(row)
filtered.sort(key=lambda r: (r["part_name"], r["part_num"]))
return filtered
def format_label(row: dict) -> str:
"""Formate létiquette de laxe vertical."""
return f"{row['part_num']}{row['part_name']}"
@@ -84,3 +100,56 @@ def plot_part_rarity(
ensure_parent_dir(destination_path)
fig.savefig(destination_path, dpi=150)
plt.close(fig)
def plot_printed_exclusive_parts(
path: Path,
destination_path: Path,
resources_dir: Path = Path("figures/rebrickable"),
columns: int = 5,
) -> None:
"""Assemble les images des pièces imprimées exclusives aux sets filtrés."""
rows = load_part_rarity(path)
selected = select_printed_exclusive(rows, resources_dir)
selected.sort(key=lambda r: (int(r.get("sample_set_year", "9999") or 9999), r["sample_set_num"], r["part_num"]))
if not selected:
return
images: List[Image.Image] = []
labels: List[str] = []
for row in selected:
image_path = resources_dir / row["sample_set_id"] / "rare_parts" / f"{row['part_num']}.jpg"
img = Image.open(image_path).convert("RGBA")
max_side = 180
ratio = min(max_side / img.width, max_side / img.height, 1.0)
if ratio < 1.0:
img = img.resize((int(img.width * ratio), int(img.height * ratio)))
images.append(img)
labels.append(f"{row.get('sample_set_year', '')}{row['sample_set_num']}")
columns = max(1, columns)
rows_count = (len(images) + columns - 1) // columns
cell_width = 220
font = ImageFont.load_default()
draw_temp = ImageDraw.Draw(Image.new("RGB", (10, 10)))
def measure(text: str) -> tuple[int, int]:
bbox = draw_temp.textbbox((0, 0), text, font=font)
return bbox[2] - bbox[0], bbox[3] - bbox[1]
text_height = max(measure(label)[1] for label in labels)
cell_height = 190 + text_height + 14
width = columns * cell_width
height = rows_count * cell_height
canvas = Image.new("RGBA", (width, height), (255, 255, 255, 255))
draw = ImageDraw.Draw(canvas)
for index, (img, label) in enumerate(zip(images, labels)):
col = index % columns
row_idx = index // columns
x = col * cell_width + (cell_width - img.width) // 2
y = row_idx * cell_height + 8
canvas.paste(img, (x, y), img)
text_width, _ = measure(label)
text_x = col * cell_width + (cell_width - text_width) // 2
text_y = y + img.height + 6
draw.text((text_x, text_y), label, fill="#111111", font=font)
ensure_parent_dir(destination_path)
canvas.convert("RGB").save(destination_path, "PNG")