-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconstellation_print.py
More file actions
112 lines (94 loc) · 4.6 KB
/
Copy pathconstellation_print.py
File metadata and controls
112 lines (94 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
"""Print an invented constellation on the S01 thermal printer.
Scatters a random starfield, links a handful of bright stars into a star-figure,
and gives the whole thing a made-up name and a snippet of fake mythology. Each
seed is a new constellation that has never existed.
Examples:
python constellation_print.py # random constellation
python constellation_print.py --seed 1234 # reproducible
python constellation_print.py --stars 9 # bright stars in the figure
python constellation_print.py --no-print
"""
from __future__ import annotations
import argparse
from math import hypot
from pathlib import Path
import random
from PIL import Image, ImageDraw
from print_common import ROOT, Card
ADJ = ["Iron", "Golden", "Silent", "Burning", "Frozen", "Ancient", "Hollow", "Crimson",
"Velvet", "Shattered", "Wandering", "Sleeping", "Electric", "Drowned", "Weeping"]
NOUN = ["Serpent", "Lantern", "Crown", "Wolf", "Harp", "Anchor", "Phoenix", "Compass",
"Goblet", "Dagger", "Loom", "Kite", "Beacon", "Jackal", "Mantis", "Ferryman"]
TRAITS = ["stubborn", "endlessly curious", "restless", "lucky in small things",
"fiercely loyal", "quietly chaotic", "good with maps", "born storytellers",
"slow to anger", "drawn to high places", "easily charmed by cats"]
SEASONS = ["spring", "summer", "autumn", "winter"]
def draw_sky(rng: random.Random, n_bright: int, w: int) -> Image.Image:
h = round(w * 0.85)
img = Image.new("L", (w, h), 0) # night sky = black
d = ImageDraw.Draw(img)
# faint background starfield
for _ in range(140):
x, y = rng.randint(0, w - 1), rng.randint(0, h - 1)
if rng.random() < 0.2:
d.ellipse((x, y, x + 1, y + 1), fill=255)
else:
d.point((x, y), fill=255)
# bright stars of the figure, kept inside a central margin and spread apart
m = int(w * 0.12)
bright: list[tuple[int, int]] = []
for _ in range(n_bright * 30):
if len(bright) >= n_bright:
break
x, y = rng.randint(m, w - m), rng.randint(m, h - m)
if all(hypot(x - bx, y - by) > w * 0.13 for bx, by in bright):
bright.append((x, y))
# connect them into a star-figure via a greedy nearest-neighbour chain
order = [0]
remaining = set(range(1, len(bright)))
while remaining:
cx, cy = bright[order[-1]]
nxt = min(remaining, key=lambda i: hypot(bright[i][0] - cx, bright[i][1] - cy))
order.append(nxt)
remaining.discard(nxt)
for a, b in zip(order, order[1:]):
d.line((*bright[a], *bright[b]), fill=255, width=1)
# draw the bright stars on top with a little twinkle cross
for x, y in bright:
r = rng.choice([3, 3, 4])
d.ellipse((x - r, y - r, x + r, y + r), fill=255)
d.line((x - r - 3, y, x + r + 3, y), fill=255, width=1)
d.line((x, y - r - 3, x, y + r + 3), fill=255, width=1)
return img
def main() -> int:
parser = argparse.ArgumentParser(description="Print an invented constellation on the S01 thermal printer.")
parser.add_argument("--stars", type=int, default=7, help="Bright stars in the figure (default: 7).")
parser.add_argument("--seed", type=int, default=None, help="Seed for a reproducible constellation.")
parser.add_argument("--out", type=Path, default=None)
parser.add_argument("--darkness", type=int, choices=range(1, 6), default=3)
parser.add_argument("--bottom-feed", type=int, default=24)
parser.add_argument("--no-print", action="store_true")
args = parser.parse_args()
if not 4 <= args.stars <= 12:
parser.error("--stars must be 4-12")
seed = args.seed if args.seed is not None else random.randrange(1, 100000)
rng = random.Random(seed)
name = f"The {rng.choice(ADJ)} {rng.choice(NOUN)}"
t1, t2 = rng.sample(TRAITS, 2)
season = rng.choice(SEASONS)
magnitude = round(rng.uniform(1.2, 4.8), 1)
print(f"{name} (seed {seed})")
card = Card()
card.title("CONSTELLATION")
card.heading(name, size=18)
card.gap(4).image(draw_sky(rng, args.stars, card.inner_w), border=True)
card.gap(4).divider(dashed=True)
card.para(f"Best seen on {season} nights, low on the horizon. "
f"Brightest star at magnitude {magnitude}. Those born beneath "
f"{name} are said to be {t1} and {t2}.", size=13)
card.footer("uncharted sky", f"{args.stars} stars - seed {seed}")
out = args.out or ROOT / f"constellation_{seed}.png"
return card.finish(out, args.bottom_feed, args.darkness, do_print=not args.no_print)
if __name__ == "__main__":
raise SystemExit(main())