Bejczy commited on
Commit
ddcc3ab
·
verified ·
1 Parent(s): 4782f92

Upload make_portrait.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. make_portrait.py +158 -0
make_portrait.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Original hero portrait: the ARCHITECT OF THE VOID.
3
+
4
+ Not a copy of anyone's character design. The figure is a silhouette packed with
5
+ bracket glyphs — the void salesman rendered as the thing he sells. Generated from
6
+ scratch: masks, glyph fill, chromatic aberration, scanlines.
7
+ """
8
+ import random
9
+ from PIL import Image, ImageDraw, ImageFont, ImageFilter
10
+
11
+ random.seed(2024)
12
+ W, H = 900, 1100
13
+ BG = (5, 6, 10)
14
+
15
+ BOLD = "/usr/share/fonts/dejavu-sans-fonts/DejaVuSans-Bold.ttf"
16
+ MONO = "/usr/share/fonts/google-noto/NotoSansMono-CondensedMedium.ttf"
17
+
18
+
19
+ def F(p, s):
20
+ try:
21
+ return ImageFont.truetype(p, s)
22
+ except Exception:
23
+ return ImageFont.load_default()
24
+
25
+
26
+ def backdrop():
27
+ img = Image.new("RGB", (W, H), BG)
28
+ d = ImageDraw.Draw(img)
29
+ for y in range(H):
30
+ t = y / H
31
+ v = int(4 + 20 * (1 - t) ** 1.4)
32
+ d.line([(0, y), (W, y)], fill=(v // 3, v // 3 + 1, v))
33
+ # distant static
34
+ for _ in range(1400):
35
+ x, y = random.randint(0, W), random.randint(0, H)
36
+ c = random.choice([(16, 18, 28), (24, 8, 20), (8, 24, 28)])
37
+ d.point((x, y), fill=c)
38
+ # faint grid
39
+ for x in range(0, W, 60):
40
+ d.line([(x, 0), (x, H)], fill=(12, 13, 20))
41
+ for y in range(0, H, 60):
42
+ d.line([(0, y), (W, y)], fill=(12, 13, 20))
43
+ return img
44
+
45
+
46
+ def figure_mask():
47
+ """Head + neck + shoulders + tapering torso silhouette."""
48
+ m = Image.new("L", (W, H), 0)
49
+ d = ImageDraw.Draw(m)
50
+ cx = W // 2
51
+ d.ellipse([cx - 134, 146, cx + 134, 462], fill=255) # head
52
+ d.polygon([(cx - 52, 430), (cx + 52, 430),
53
+ (cx + 62, 556), (cx - 62, 556)], fill=255) # neck
54
+ d.polygon([(cx - 62, 540), (cx + 62, 540), # shoulders
55
+ (cx + 238, 604), (cx + 282, 1099),
56
+ (cx - 282, 1099), (cx - 238, 604)], fill=255)
57
+ return m
58
+
59
+
60
+ def glyph_fill(mask):
61
+ """Pack the silhouette with bracket glyphs."""
62
+ lay = Image.new("RGB", (W, H), (0, 0, 0))
63
+ d = ImageDraw.Draw(lay)
64
+ glyphs = ["[[", "]]", "$$", "[]", "[", "]", "$", "#", "@", "!", "%", "&"]
65
+ sizes = [14, 17, 20, 24, 28, 34, 42]
66
+ y = 140
67
+ while y < H:
68
+ s = random.choice(sizes)
69
+ f = F(MONO, s)
70
+ x = random.randint(-40, 4)
71
+ while x < W:
72
+ g = random.choice(glyphs)
73
+ shade = random.randint(130, 255)
74
+ tint = random.choice([(shade, shade, shade),
75
+ (0, shade, shade),
76
+ (shade, 0, shade // 2),
77
+ (shade, shade // 2, 0)])
78
+ d.text((x, y), g, font=f, fill=tint)
79
+ x += d.textlength(g, font=f) + random.randint(0, 7)
80
+ y += s + random.randint(-2, 4)
81
+ lay = lay.filter(ImageFilter.GaussianBlur(0.4))
82
+ out = Image.new("RGB", (W, H), (0, 0, 0))
83
+ out.paste(lay, (0, 0), mask)
84
+ return out
85
+
86
+
87
+ def chroma(img, dx=5):
88
+ r, g, b = img.split()
89
+ m = Image.new("L", (W, H), 0)
90
+ m = ImageChops_lighter(m, r)
91
+ out = img.copy()
92
+ for col, off in (((0, 235, 245), -dx), ((240, 20, 160), dx)):
93
+ tint = Image.new("RGB", (W, H), col)
94
+ shifted = m.transform((W, H), Image.AFFINE, (1, 0, -off, 0, 1, 0))
95
+ out = Image.composite(tint, out, shifted)
96
+ out = Image.composite(img, out, m)
97
+ return out
98
+
99
+
100
+ def ImageChops_lighter(a, b):
101
+ from PIL import ImageChops
102
+ return ImageChops.lighter(a, b)
103
+
104
+
105
+ def eyes(img):
106
+ d = ImageDraw.Draw(img)
107
+ cx = W // 2
108
+ for ex in (cx - 74, cx + 26):
109
+ d.rectangle([ex, 268, ex + 48, 296], fill=(255, 252, 240))
110
+ d.rectangle([ex - 4, 262, ex + 52, 302], outline=(0, 220, 235))
111
+ d.rectangle([cx - 96, 258, cx + 96, 306], outline=(240, 0, 150))
112
+ # grin
113
+ d.arc([cx - 78, 330, cx + 78, 430], start=20, end=160, fill=(255, 250, 238), width=7)
114
+ return img
115
+
116
+
117
+ def sigils(img):
118
+ d = ImageDraw.Draw(img)
119
+ d.text((40, 40), "[[", font=F(BOLD, 150), fill=(30, 34, 52))
120
+ d.text((W - 200, H - 210), "]]", font=F(BOLD, 150), fill=(30, 34, 52))
121
+ d.text((W - 150, 120), "$$$", font=F(MONO, 42), fill=(190, 30, 105))
122
+ d.text((46, 210), "KROMER", font=F(MONO, 26), fill=(38, 42, 60))
123
+ d.text((46, H - 90), "ARCHITECT OF THE VOID", font=F(MONO, 24), fill=(150, 170, 190))
124
+ return img
125
+
126
+
127
+ def finish(img):
128
+ d = ImageDraw.Draw(img)
129
+ # glitch bands, outside the face
130
+ for _ in range(22):
131
+ y0 = random.choice([random.randint(470, H - 30), random.randint(0, 130)])
132
+ h = random.randint(3, 20)
133
+ band = img.crop((0, y0, W, min(y0 + h, H)))
134
+ img.paste(band, (random.randint(-26, 26), y0))
135
+ for y in range(0, H, 4):
136
+ d.line([(0, y), (W, y)], fill=(0, 0, 0))
137
+ m = Image.new("L", (W, H), 0)
138
+ ImageDraw.Draw(m).ellipse([-W * .3, -H * .25, W * 1.3, H * 1.25], fill=255)
139
+ m = m.filter(ImageFilter.GaussianBlur(150))
140
+ return Image.composite(img, Image.new("RGB", (W, H), (0, 0, 0)), m)
141
+
142
+
143
+ def main():
144
+ img = backdrop()
145
+ mask = figure_mask()
146
+ fig = glyph_fill(mask)
147
+ img.paste(fig, (0, 0), mask)
148
+ img = chroma(img, 6)
149
+ img = eyes(img)
150
+ img = sigils(img)
151
+ img = finish(img)
152
+ out = "/home/ryzen/foryou/spamton-portrait.png"
153
+ img.save(out)
154
+ print("wrote", out, img.size)
155
+
156
+
157
+ if __name__ == "__main__":
158
+ main()