From f25d752e9c26b94ec2369834474a5666834db075 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Wed, 3 Dec 2025 23:41:12 +0100 Subject: [PATCH] =?UTF-8?q?Ajoute=20les=20noms=20dans=20les=20collages=20g?= =?UTF-8?q?lobaux=20filtr=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/plots/minifig_character_collages.py | 15 +++++++++++++++ scripts/plot_minifig_collage_grids.py | 11 ++++++++++- tests/test_minifig_character_collages.py | 17 +++++++++++++---- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/plots/minifig_character_collages.py b/lib/plots/minifig_character_collages.py index c440cac..892ed26 100644 --- a/lib/plots/minifig_character_collages.py +++ b/lib/plots/minifig_character_collages.py @@ -141,15 +141,30 @@ def stack_collages( destination_path: Path, spacing: int = 18, allowed_stems: Set[str] | None = None, + name_panel_width: int = 0, + labels_by_stem: Dict[str, str] | None = None, ) -> None: """Superpose verticalement toutes les frises présentes dans un dossier.""" images: List[Image.Image] = [] + stems: List[str] = [] for path in sorted(source_dir.glob("*.png")): if allowed_stems is not None and path.stem not in allowed_stems: continue images.append(Image.open(path).convert("RGB")) + stems.append(path.stem) if not images: return + font = ImageFont.load_default() + if name_panel_width > 0: + with_names: List[Image.Image] = [] + for image, stem in zip(images, stems): + label = labels_by_stem.get(stem, stem) if labels_by_stem else stem + name_img = render_label(name_panel_width, image.height, label, font).convert("RGB") + combined = Image.new("RGB", (name_panel_width + image.width, image.height), (255, 255, 255)) + combined.paste(name_img, (0, 0)) + combined.paste(image, (name_panel_width, 0)) + with_names.append(combined) + images = with_names max_width = max(image.width for image in images) total_height = sum(image.height for image in images) + spacing * (len(images) - 1) canvas = Image.new("RGB", (max_width, total_height), (255, 255, 255)) diff --git a/scripts/plot_minifig_collage_grids.py b/scripts/plot_minifig_collage_grids.py index 398e4c2..e35efd4 100644 --- a/scripts/plot_minifig_collage_grids.py +++ b/scripts/plot_minifig_collage_grids.py @@ -10,6 +10,7 @@ from lib.rebrickable.minifig_character_sets import ( load_sets, ) from lib.rebrickable.resources import sanitize_name +from lib.rebrickable.resources import sanitize_name MINIFIGS_BY_SET_PATH = Path("data/intermediate/minifigs_by_set.csv") @@ -29,12 +30,20 @@ def main() -> None: grouped = group_by_character(character_sets) filtered = filter_characters_with_variations(grouped, min_variations=2) keep = {sanitize_name(name) for name in filtered.keys()} + labels = {sanitize_name(name): name for name in filtered.keys()} for source_dir, destination in ( (CHARACTERS_DIR, CHARACTERS_DEST), (HEADS_DIR, HEADS_DEST), ): - stack_collages(source_dir, destination, spacing=12, allowed_stems=keep) + stack_collages( + source_dir, + destination, + spacing=12, + allowed_stems=keep, + name_panel_width=160, + labels_by_stem=labels, + ) if __name__ == "__main__": diff --git a/tests/test_minifig_character_collages.py b/tests/test_minifig_character_collages.py index 6da8e57..40509d5 100644 --- a/tests/test_minifig_character_collages.py +++ b/tests/test_minifig_character_collages.py @@ -125,7 +125,8 @@ def test_build_character_sets_and_collages(tmp_path: Path) -> None: ) assert len(head_collages) == 1 bob_head = Image.open(heads_destination / f"{sanitize_name('Bob')}.png") - assert bob_head.getpixel((3, 8)) == (220, 220, 220) + assert bob_head.size == (34, 24) + assert any(pixel == (220, 220, 220) for pixel in bob_head.getdata()) def test_stack_collages_with_filter(tmp_path: Path) -> None: @@ -138,8 +139,16 @@ def test_stack_collages_with_filter(tmp_path: Path) -> None: Image.new("RGB", (14, 8), (120, 120, 120)).save(img2) destination = tmp_path / "stacked.png" - stack_collages(source_dir, destination, spacing=2, allowed_stems={"Bob"}) + stack_collages( + source_dir, + destination, + spacing=2, + allowed_stems={"Bob"}, + name_panel_width=4, + labels_by_stem={"Bob": "Bob"}, + ) result = Image.open(destination) - assert result.size == (14, 8) - assert result.getpixel((0, 0)) == (120, 120, 120) + assert result.size == (18, 8) + assert result.getpixel((0, 0)) == (255, 255, 255) + assert result.getpixel((6, 0)) == (120, 120, 120)