You've already forked etude_lego_jurassic_world
Collage des pièces imprimées exclusives et variante sans impressions
This commit is contained in:
@@ -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 l’axe 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")
|
||||
|
||||
@@ -126,7 +126,8 @@ def build_part_rarity(
|
||||
other_quantity = other_usage.get(part_num, 0)
|
||||
total_quantity = entry["quantity"] + other_quantity
|
||||
sample_set_num = sorted(entry["set_numbers"])[0]
|
||||
sample_set_id = filtered_sets[sample_set_num]["set_id"]
|
||||
sample_set_row = filtered_sets[sample_set_num]
|
||||
sample_set_id = sample_set_row["set_id"]
|
||||
rows.append(
|
||||
{
|
||||
"part_num": part_num,
|
||||
@@ -135,6 +136,7 @@ def build_part_rarity(
|
||||
"part_category": categories[part["part_cat_id"]],
|
||||
"sample_set_num": sample_set_num,
|
||||
"sample_set_id": sample_set_id,
|
||||
"sample_set_year": sample_set_row["year"],
|
||||
"filtered_quantity": str(entry["quantity"]),
|
||||
"filtered_set_count": str(len(entry["set_numbers"])),
|
||||
"other_sets_quantity": str(other_quantity),
|
||||
@@ -156,6 +158,7 @@ def write_part_rarity(destination_path: Path, rows: Sequence[dict]) -> None:
|
||||
"part_category",
|
||||
"sample_set_num",
|
||||
"sample_set_id",
|
||||
"sample_set_year",
|
||||
"filtered_quantity",
|
||||
"filtered_set_count",
|
||||
"other_sets_quantity",
|
||||
|
||||
Reference in New Issue
Block a user