Ajoute les noms dans les collages globaux filtrés
This commit is contained in:
parent
316e1d89a7
commit
f25d752e9c
@ -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))
|
||||
|
||||
@ -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__":
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user