Setting the file. One moment. Make Contact Sheet · Hatch Pet · openai/skills · Skills Docs16.11
Validate Atlas
scripts/make_contact_sheet.py
scripts/make_contact_sheet.py
Python·96 lines·3 KB
CELL_WIDTH
=
192
14CELL_HEIGHT = 208
15LABEL_HEIGHT = 22
16ROW_NAMES = [
17 "idle",
18 "running-right",
19 "running-left",
20 "waving",
21 "jumping",
22 "failed",
23 "waiting",
24 "running",
25 "review",
26]
27USED_COUNTS = [6, 8, 8, 4, 5, 8, 6, 6, 6]
28
29
30def checker(size: tuple[int, int], square: int = 16) -> Image.Image:
31 image = Image.new("RGB", size, "#ffffff")
32 draw = ImageDraw.Draw(image)
33 for y in range(0, size[1], square):
34 for x in range(0, size[0], square):
35 if (x // square + y // square) % 2:
36 draw.rectangle((x, y, x + square - 1, y + square - 1), fill="#e8e8e8")
37 return image
38
39
40def main() -> None:
41 parser = argparse.ArgumentParser(description=__doc__)
42 parser.add_argument("atlas")
43 parser.add_argument("--output", required=True)
44 parser.add_argument("--scale", type=float, default=0.5)
45 args = parser.parse_args()
46
47 with Image.open(Path(args.atlas).expanduser().resolve()) as opened:
48 atlas = opened.convert("RGBA")
49
50 cell_w = max(1, round(CELL_WIDTH * args.scale))
51 cell_h = max(1, round(CELL_HEIGHT * args.scale))
52 width = COLUMNS * cell_w
53 height = ROWS * (cell_h + LABEL_HEIGHT)
54 sheet = Image.new("RGB", (width, height), "#f7f7f7")
55 draw = ImageDraw.Draw(sheet)
56 font = ImageFont.load_default()
57
58 for row in range(ROWS):
59 y = row * (cell_h + LABEL_HEIGHT)
60 draw.rectangle((0, y, width, y + LABEL_HEIGHT - 1), fill="#111111")
61 draw.text((6, y + 5), f"row {row}: {ROW_NAMES[row]}", fill="#ffffff", font=font)
62 draw.text(
63 (width - 92, y + 5),
64 f"{USED_COUNTS[row]} frames",
65 fill="#ffffff",
66 font=font,
67 )
68 for column in range(COLUMNS):
69 crop = atlas.crop(
70 (
71 column * CELL_WIDTH,
72 row * CELL_HEIGHT,
73 (column + 1) * CELL_WIDTH,
74 (row + 1) * CELL_HEIGHT,
75 )
76 )
77 crop = crop.resize((cell_w, cell_h), Image.Resampling.LANCZOS)
78 bg = checker((cell_w, cell_h))
79 bg.paste(crop, (0, 0), crop)
80 x = column * cell_w
81 sheet.paste(bg, (x, y + LABEL_HEIGHT))
82 outline = "#18a058" if column < USED_COUNTS[row] else "#cc3344"
83 draw.rectangle(
84 (x, y + LABEL_HEIGHT, x + cell_w - 1, y + LABEL_HEIGHT + cell_h - 1),
85 outline=outline,
86 )
87 draw.text((x + 4, y + LABEL_HEIGHT + 4), str(column), fill="#111111", font=font)
88
89 output = Path(args.output).expanduser().resolve()
90 output.parent.mkdir(parents=True, exist_ok=True)
91 sheet.save(output)
92 print(f"wrote {output}")
93
94
95if __name__ == "__main__":
96 main()