coderdecoder01 commited on
Commit
6d0b2e0
·
verified ·
1 Parent(s): dfdf81c

Add invitation renderer (Indian style, 1080x1920 MP4)

Browse files
Files changed (1) hide show
  1. render_invitation.py +517 -0
render_invitation.py ADDED
@@ -0,0 +1,517 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Renders an Indian-style animated retirement invitation: 1080x1920, 30fps, H.264 MP4.
4
+ Maroon/gold palette, rotating mandala, marigold toran, flickering diyas, ornate frame.
5
+ """
6
+ import os, math, subprocess, urllib.request, random
7
+ import numpy as np
8
+ from PIL import Image, ImageDraw, ImageFont, ImageFilter, features
9
+
10
+ W, H, FPS = 1080, 1920, 30
11
+ OUT = "/tmp/mamta_retirement_invitation.mp4"
12
+ POSTER = "/tmp/poster.jpg"
13
+ FDIR = "/tmp/fonts"
14
+ os.makedirs(FDIR, exist_ok=True)
15
+
16
+ BG_TOP, BG_BOT = (78, 15, 36), (44, 7, 20)
17
+ GOLD, GOLD_D, GOLD_L = (232, 193, 115), (201, 150, 47), (245, 222, 166)
18
+ CREAM, MARIGOLD, ORANGE = (253, 243, 224), (240, 160, 42), (226, 112, 28)
19
+ SOFT = (244, 229, 203)
20
+
21
+ # ----------------------------------------------------------------- event text
22
+ EV = dict(
23
+ name="Mamta",
24
+ hindi="\u0936\u0941\u092d \u0938\u0947\u0935\u093e\u0928\u093f\u0935\u0943\u0924\u094d\u0924\u093f", # शुभ सेवानिवृत्ति
25
+ kicker1_en="WITH GREAT JOY",
26
+ script1="You\u2019re Invited",
27
+ sub1="a celebration of a beautiful journey",
28
+ kicker2="IN HONOUR OF",
29
+ body2="on the joyous occasion of her retirement",
30
+ small2="30 SEPTEMBER 2026",
31
+ card_title="Celebratory Lunch",
32
+ when_label="WHEN",
33
+ when_1="Wednesday, 30 September 2026",
34
+ when_2="Lunch at 1:30 pm",
35
+ where_label="WHERE",
36
+ where_1="New Rose Common",
37
+ sub3="Your presence will make it complete",
38
+ quote=["May the years ahead be as golden", "as the ones you so generously gave."],
39
+ script5="Come share a memory",
40
+ body5="a laugh, a toast, and something sweet with",
41
+ rsvp_label="RSVP",
42
+ rsvp_value="Kindly reply to the family",
43
+ kicker6="WE CAN\u2019T WAIT TO",
44
+ script6="Celebrate With You",
45
+ sig="With love, the family",
46
+ )
47
+
48
+ # ----------------------------------------------------------------- fonts
49
+ FONT_URLS = {
50
+ "script": ["https://raw.githubusercontent.com/google/fonts/main/ofl/greatvibes/GreatVibes-Regular.ttf"],
51
+ "serif": ["https://raw.githubusercontent.com/google/fonts/main/ofl/playfairdisplay/PlayfairDisplay%5Bwght%5D.ttf",
52
+ "https://raw.githubusercontent.com/google/fonts/main/ofl/playfairdisplay/static/PlayfairDisplay-Regular.ttf"],
53
+ "body": ["https://raw.githubusercontent.com/google/fonts/main/ofl/cormorantgaramond/CormorantGaramond%5Bwght%5D.ttf",
54
+ "https://raw.githubusercontent.com/google/fonts/main/ofl/cormorantgaramond/static/CormorantGaramond-Regular.ttf"],
55
+ "bodyi": ["https://raw.githubusercontent.com/google/fonts/main/ofl/cormorantgaramond/CormorantGaramond-Italic%5Bwght%5D.ttf",
56
+ "https://raw.githubusercontent.com/google/fonts/main/ofl/cormorantgaramond/static/CormorantGaramond-Italic.ttf"],
57
+ "hindi": ["https://raw.githubusercontent.com/google/fonts/main/ofl/notoserifdevanagari/NotoSerifDevanagari%5Bwght%5D.ttf",
58
+ "https://raw.githubusercontent.com/google/fonts/main/ofl/notoserifdevanagari/static/NotoSerifDevanagari-Regular.ttf"],
59
+ }
60
+ FPATH = {}
61
+ for key, urls in FONT_URLS.items():
62
+ for i, u in enumerate(urls):
63
+ dest = os.path.join(FDIR, "%s_%d.ttf" % (key, i))
64
+ try:
65
+ req = urllib.request.Request(u, headers={"User-Agent": "Mozilla/5.0"})
66
+ with urllib.request.urlopen(req, timeout=60) as r, open(dest, "wb") as fh:
67
+ fh.write(r.read())
68
+ FPATH[key] = dest
69
+ print("FONT OK %-7s %8d %s" % (key, os.path.getsize(dest), u.split("/")[-1]))
70
+ break
71
+ except Exception as e:
72
+ print("FONT FAIL %-7s %s" % (key, repr(e)[:90]))
73
+ HAS_RAQM = bool(features.check("raqm"))
74
+ print("RAQM (complex text shaping):", HAS_RAQM)
75
+
76
+ _fc = {}
77
+ def F(kind, size):
78
+ key = (kind, size)
79
+ if key in _fc:
80
+ return _fc[key]
81
+ path = FPATH.get(kind) or FPATH.get("serif") or FPATH.get("body")
82
+ f = ImageFont.truetype(path, size)
83
+ _fc[key] = f
84
+ return f
85
+
86
+ # ----------------------------------------------------------------- text helper
87
+ def text_layer(lines, kind, size, color, tracking=0, line_h=None):
88
+ """Render ink-cropped RGBA layer of centred text."""
89
+ f = F(kind, size)
90
+ tmp = Image.new("RGBA", (8, 8)); td = ImageDraw.Draw(tmp)
91
+ lh = line_h or int(size * 1.30)
92
+ widths = []
93
+ for ln in lines:
94
+ if tracking:
95
+ widths.append(sum(td.textlength(c, font=f) for c in ln) + tracking * max(0, len(ln) - 1))
96
+ else:
97
+ widths.append(td.textlength(ln, font=f))
98
+ pad = int(size * 0.9) + 14
99
+ lw = int(max(widths)) + 2 * pad
100
+ lht = int(lh * len(lines)) + 2 * pad
101
+ lay = Image.new("RGBA", (lw, lht), (0, 0, 0, 0))
102
+ d = ImageDraw.Draw(lay)
103
+ for i, ln in enumerate(lines):
104
+ base = pad + lh * i + lh * 0.80
105
+ if tracking:
106
+ x = (lw - widths[i]) / 2.0
107
+ for ch in ln:
108
+ d.text((x, base), ch, font=f, fill=color + (255,), anchor="ls")
109
+ x += td.textlength(ch, font=f) + tracking
110
+ else:
111
+ d.text((lw / 2.0, pad + lh * i + lh / 2.0), ln, font=f, fill=color + (255,), anchor="mm")
112
+ bb = lay.getbbox()
113
+ return lay.crop(bb) if bb else lay
114
+
115
+ def alpha_scale(sprite, a):
116
+ if a >= 0.999:
117
+ return sprite
118
+ arr = np.array(sprite, dtype=np.uint16)
119
+ arr[..., 3] = (arr[..., 3] * a).astype(np.uint16)
120
+ return Image.fromarray(arr.astype(np.uint8), "RGBA")
121
+
122
+ def alpha_variants(sprite, n=12):
123
+ base = np.array(sprite, dtype=np.uint16)
124
+ out = []
125
+ for i in range(n):
126
+ arr = base.copy()
127
+ arr[..., 3] = (arr[..., 3] * (i / float(n - 1))).astype(np.uint16)
128
+ out.append(Image.fromarray(arr.astype(np.uint8), "RGBA"))
129
+ return out
130
+
131
+ def paste(dst, lay, cx, cy, alpha, dx=0.0, dy=0.0, anchor="c"):
132
+ if alpha <= 0.004:
133
+ return
134
+ im = alpha_scale(lay, alpha) if alpha < 0.999 else lay
135
+ if anchor == "c":
136
+ x = int(cx - lay.size[0] / 2 + dx); y = int(cy - lay.size[1] / 2 + dy)
137
+ else: # top-left
138
+ x = int(cx + dx); y = int(cy + dy)
139
+ if x + im.size[0] < 0 or y + im.size[1] < 0 or x > W or y > H:
140
+ return
141
+ dst.alpha_composite(im, (x, y))
142
+
143
+ def rule_strip(w, alpha, thickness=2):
144
+ ramp = np.linspace(0.0, 1.0, max(2, w))
145
+ a = (np.sin(np.pi * ramp) ** 1.4 * 255 * alpha).astype(np.uint8)
146
+ st = np.zeros((thickness, max(2, w), 4), dtype=np.uint8)
147
+ st[..., 0], st[..., 1], st[..., 2] = GOLD[0], GOLD[1], GOLD[2]
148
+ st[..., 3] = a
149
+ return Image.fromarray(st, "RGBA")
150
+
151
+ # ----------------------------------------------------------------- furniture
152
+ def build_bg():
153
+ y = np.linspace(0, 1, H)[:, None]
154
+ bg = np.array(BG_TOP)[None, None, :] * (1 - y[..., None]) + np.array(BG_BOT)[None, None, :] * y[..., None]
155
+ bg = np.repeat(bg, W, axis=1).astype(np.float32)
156
+ yy, xx = np.mgrid[0:H, 0:W].astype(np.float32)
157
+ d = np.sqrt(((xx - W * 0.5) / (W * 0.80)) ** 2 + ((yy - H * 0.40) / (H * 0.52)) ** 2)
158
+ glow = np.clip(1 - d, 0, 1) ** 2.3
159
+ bg += glow[..., None] * np.array([118, 58, 18], dtype=np.float32)[None, None, :]
160
+ return Image.fromarray(np.clip(bg, 0, 255).astype(np.uint8), "RGB")
161
+
162
+ def build_frame():
163
+ S = 2
164
+ im = Image.new("RGBA", (W * S, H * S), (0, 0, 0, 0)); d = ImageDraw.Draw(im)
165
+ for m, col, a, wd in ((38, GOLD, 215, 2.2), (54, GOLD_L, 105, 1.0)):
166
+ d.rectangle([m * S, m * S, (W - m) * S, (H - m) * S], outline=col + (a,), width=int(wd * S))
167
+ for cx, cy, sx, sy in ((72, 72, 1, 1), (W - 72, 72, -1, 1), (72, H - 72, 1, -1), (W - 72, H - 72, -1, -1)):
168
+ L = 44
169
+ d.line([cx * S, cy * S, (cx + L * sx) * S, cy * S], fill=GOLD_L + (170,), width=int(1.5 * S))
170
+ d.line([cx * S, cy * S, cx * S, (cy + L * sy) * S], fill=GOLD_L + (170,), width=int(1.5 * S))
171
+ for k in (1, 2, 3):
172
+ px, py = cx + 11 * k * sx, cy + 11 * k * sy
173
+ d.ellipse([(px - 3) * S, (py - 3) * S, (px + 3) * S, (py + 3) * S], fill=MARIGOLD + (150,))
174
+ return im.resize((W, H), Image.LANCZOS)
175
+
176
+ def build_toran(height=210):
177
+ im = Image.new("RGBA", (W, height), (0, 0, 0, 0)); d = ImageDraw.Draw(im)
178
+ d.rectangle([0, 0, W, 14], fill=(28, 5, 14, 255))
179
+ for i in range(0, W, 22):
180
+ d.ellipse([i + 3, 10, i + 19, 26], fill=GOLD_D + (230,))
181
+ for k in range(4):
182
+ x0, x1 = W * k / 4.0, W * (k + 1) / 4.0
183
+ for i in range(48):
184
+ t = i / 47.0
185
+ x = x0 + (x1 - x0) * t
186
+ yy = 52 + math.sin(math.pi * t) * 74
187
+ r = 12.5
188
+ col = MARIGOLD if i % 2 == 0 else ORANGE
189
+ d.ellipse([x - r, yy - r, x + r, yy + r], fill=col + (238,))
190
+ d.ellipse([x - r * 0.34, yy - r * 0.55, x + r * 0.18, yy - r * 0.12], fill=(255, 236, 196, 130))
191
+ jx = x1 if k < 3 else None
192
+ if jx:
193
+ d.polygon([(jx, 108), (jx - 13, 92), (jx, 74), (jx + 13, 92)], fill=(58, 122, 74, 210))
194
+ for x in (W * 0.25, W * 0.5, W * 0.75):
195
+ d.line([x, 118, x, 176], fill=GOLD_L + (170,), width=2)
196
+ d.ellipse([x - 11, 172, x + 11, 194], fill=ORANGE + (238,))
197
+ d.ellipse([x - 4, 177, x + 2, 183], fill=(255, 236, 196, 150))
198
+ return im
199
+
200
+ def build_mandala(size=1040):
201
+ S = 2; s = size * S
202
+ im = Image.new("RGBA", (s, s), (0, 0, 0, 0)); d = ImageDraw.Draw(im)
203
+ c = s / 2.0
204
+ for rf, wd, a in ((0.480, 2.4, 90), (0.452, 1.1, 55), (0.300, 1.1, 45), (0.196, 2.0, 70), (0.120, 1.1, 45)):
205
+ r = s * rf / 2.0
206
+ d.ellipse([c - r, c - r, c + r, c + r], outline=GOLD_L + (a,), width=int(wd * S))
207
+ for i in range(24):
208
+ ang = 360.0 * i / 24
209
+ pw, ph = int(s * 0.115), int(s * 0.038)
210
+ p = Image.new("RGBA", (pw, ph), (0, 0, 0, 0))
211
+ ImageDraw.Draw(p).ellipse([0, 0, pw - 1, ph - 1], outline=GOLD_L + (85,), width=int(1.5 * S))
212
+ p = p.rotate(-ang, expand=True, resample=Image.BICUBIC)
213
+ r = s * 0.368 / 2.0
214
+ x = c + r * math.cos(math.radians(ang)) - p.size[0] / 2.0
215
+ y = c + r * math.sin(math.radians(ang)) - p.size[1] / 2.0
216
+ im.alpha_composite(p, (int(x), int(y)))
217
+ for i in range(48):
218
+ ang = 360.0 * i / 48
219
+ r = s * 0.248 / 2.0
220
+ x, y = c + r * math.cos(math.radians(ang)), c + r * math.sin(math.radians(ang))
221
+ d.ellipse([x - 3.2 * S, y - 3.2 * S, x + 3.2 * S, y + 3.2 * S], fill=MARIGOLD + (105,))
222
+ return im.resize((size, size), Image.LANCZOS)
223
+
224
+ def build_diya(size=200):
225
+ S = 2; s = size * S
226
+ im = Image.new("RGBA", (s, s), (0, 0, 0, 0)); d = ImageDraw.Draw(im)
227
+ d.chord([s * 0.10, s * 0.50, s * 0.90, s * 0.98], start=0, end=180, fill=GOLD_D + (255,))
228
+ d.chord([s * 0.10, s * 0.50, s * 0.90, s * 0.98], start=0, end=180, outline=GOLD_L + (255,), width=int(3 * S))
229
+ d.arc([s * 0.10, s * 0.50, s * 0.90, s * 0.98], start=180, end=360, fill=GOLD_L + (220,), width=int(3 * S))
230
+ fx, fy = s * 0.5, s * 0.50
231
+ d.polygon([(fx, fy - s * 0.30), (fx - s * 0.070, fy), (fx + s * 0.070, fy)], fill=(255, 206, 108, 255))
232
+ d.ellipse([fx - s * 0.070, fy - s * 0.085, fx + s * 0.070, fy + s * 0.085], fill=(255, 206, 108, 255))
233
+ d.polygon([(fx, fy - s * 0.175), (fx - s * 0.033, fy - s * 0.005), (fx + s * 0.033, fy - s * 0.005)], fill=(255, 250, 226, 255))
234
+ return im.resize((size, size), Image.LANCZOS)
235
+
236
+ def build_glow(size=420, radius=170.0):
237
+ yy, xx = np.mgrid[0:size, 0:size]
238
+ c = (size - 1) / 2.0
239
+ d = np.sqrt((xx - c) ** 2 + (yy - c) ** 2) / radius
240
+ g = np.clip(1 - d, 0, 1) ** 2.4
241
+ arr = np.zeros((size, size, 4), dtype=np.uint8)
242
+ arr[..., 0], arr[..., 1], arr[..., 2] = 255, 176, 70
243
+ arr[..., 3] = (g * 195).astype(np.uint8)
244
+ return Image.fromarray(arr, "RGBA")
245
+
246
+ def build_mote(size=26):
247
+ yy, xx = np.mgrid[0:size, 0:size]
248
+ c = (size - 1) / 2.0
249
+ r = np.sqrt((xx - c) ** 2 + (yy - c) ** 2) / c
250
+ g = np.clip(1 - r, 0, 1) ** 2.0
251
+ arr = np.zeros((size, size, 4), dtype=np.uint8)
252
+ arr[..., 0], arr[..., 1], arr[..., 2] = 244, 214, 150
253
+ arr[..., 3] = (g * 210).astype(np.uint8)
254
+ return Image.fromarray(arr, "RGBA")
255
+
256
+ def build_lotus(size=124):
257
+ im = Image.new("RGBA", (size, size), (0, 0, 0, 0))
258
+ for i in range(6):
259
+ pw, ph = int(size * 0.86), int(size * 0.26)
260
+ p = Image.new("RGBA", (pw, ph), (0, 0, 0, 0))
261
+ ImageDraw.Draw(p).ellipse([0, 0, pw - 1, ph - 1], outline=GOLD_L + (215,), width=3)
262
+ p = p.rotate(-(i * 30), expand=True, resample=Image.BICUBIC)
263
+ im.alpha_composite(p, (size // 2 - p.size[0] // 2, size // 2 - p.size[1] // 2))
264
+ d = ImageDraw.Draw(im)
265
+ d.ellipse([size / 2 - 8, size / 2 - 8, size / 2 + 8, size / 2 + 8], fill=MARIGOLD + (235,))
266
+ return im
267
+
268
+ def build_monogram(D=600):
269
+ im = Image.new("RGBA", (D, D), (0, 0, 0, 0)); d = ImageDraw.Draw(im)
270
+ c = D / 2.0
271
+ d.ellipse([5, 5, D - 5, D - 5], outline=GOLD + (215,), width=3)
272
+ d.ellipse([21, 21, D - 21, D - 21], outline=GOLD_L + (120,), width=2)
273
+ d.ellipse([34, 34, D - 34, D - 34], outline=GOLD_L + (70,), width=1)
274
+ for i in range(14):
275
+ ang = 360.0 * i / 14
276
+ pw, ph = int(D * 0.19), int(D * 0.075)
277
+ p = Image.new("RGBA", (pw, ph), (0, 0, 0, 0))
278
+ ImageDraw.Draw(p).ellipse([0, 0, pw - 1, ph - 1], outline=GOLD_L + (120,), width=2)
279
+ p = p.rotate(-ang, expand=True, resample=Image.BICUBIC)
280
+ r = D * 0.415
281
+ x = c + r * math.cos(math.radians(ang)) - p.size[0] / 2.0
282
+ y = c + r * math.sin(math.radians(ang)) - p.size[1] / 2.0
283
+ im.alpha_composite(p, (int(x), int(y)))
284
+ lay = text_layer([EV["name"][0].upper()], "script", int(D * 0.60), CREAM)
285
+ im.alpha_composite(lay, (int(c - lay.size[0] / 2), int(c - lay.size[1] / 2)))
286
+ return im
287
+
288
+ def make_card(width, title, sections, title_size=66, label_size=26, value_size=46, alpha=205):
289
+ Hc = 1400
290
+ card = Image.new("RGBA", (width, Hc), (0, 0, 0, 0))
291
+ S = 2
292
+ tmp = Image.new("RGBA", (width * S, Hc * S), (0, 0, 0, 0))
293
+ ImageDraw.Draw(tmp).rounded_rectangle([0, 0, width * S - 1, Hc * S - 1], radius=int(16 * S),
294
+ fill=(60, 11, 28, alpha), outline=GOLD + (235,), width=int(2.2 * S))
295
+ card.alpha_composite(tmp.resize((width, Hc), Image.LANCZOS), (0, 0))
296
+ y = 74
297
+ if title:
298
+ tl = text_layer([title], "script", title_size, GOLD)
299
+ card.alpha_composite(tl, (int(width / 2 - tl.size[0] / 2), int(y)))
300
+ y += tl.size[1] + 30
301
+ rw = int(width * 0.44)
302
+ card.alpha_composite(rule_strip(rw, 0.9, 2), (int(width / 2 - rw / 2), int(y)))
303
+ y += 46
304
+ for si, sec in enumerate(sections):
305
+ if si:
306
+ rw = int(width * 0.34)
307
+ card.alpha_composite(rule_strip(rw, 0.55, 1), (int(width / 2 - rw / 2), int(y)))
308
+ y += 38
309
+ ll = text_layer([sec["label"]], "serif", label_size, GOLD, tracking=11)
310
+ card.alpha_composite(ll, (int(width / 2 - ll.size[0] / 2), int(y)))
311
+ y += ll.size[1] + 22
312
+ for v in sec["values"]:
313
+ vl = text_layer([v], "body", value_size, CREAM)
314
+ card.alpha_composite(vl, (int(width / 2 - vl.size[0] / 2), int(y)))
315
+ y += vl.size[1] + 12
316
+ y += 18
317
+ used = y + 40
318
+ tl2 = build_lotus(64)
319
+ card.alpha_composite(tl2, (int(width / 2 - 32), int(used - 20)))
320
+ return card.crop((0, 0, width, min(Hc, used + 54)))
321
+
322
+ # ----------------------------------------------------------------- build assets
323
+ print("building assets ...")
324
+ BG = build_bg().convert("RGBA")
325
+ FRAME = build_frame()
326
+ TORAN = build_toran()
327
+ MANDALA = alpha_scale(build_mandala(1040), 0.19)
328
+ DIYA = build_diya(200)
329
+ LOTUS = build_lotus(120)
330
+ LOTUS_S = build_lotus(64)
331
+ GLOWS = alpha_variants(build_glow(430, 175.0), 12)
332
+ MOTE_V = alpha_variants(build_mote(26), 12)
333
+ MOTE_S = alpha_variants(build_mote(15), 12)
334
+
335
+ random.seed(11)
336
+ MOTES = [dict(x=random.uniform(70, W - 70), y=random.uniform(0, H), sp=random.uniform(58, 132),
337
+ sw=random.uniform(0.25, 0.7), ph=random.uniform(0, 6.28), big=random.random() < 0.45)
338
+ for _ in range(16)]
339
+
340
+ kicker1 = text_layer([EV["hindi"]], "hindi", 44, GOLD) if HAS_RAQM else \
341
+ text_layer([EV["kicker1_en"]], "serif", 34, GOLD, tracking=13)
342
+ if HAS_RAQM:
343
+ k1_extra = text_layer([EV["kicker1_en"]], "serif", 26, GOLD, tracking=11)
344
+ else:
345
+ k1_extra = None
346
+
347
+ L_script1 = text_layer([EV["script1"]], "script", 158, GOLD)
348
+ L_sub1 = text_layer([EV["sub1"]], "bodyi", 46, SOFT)
349
+ L_kick2 = text_layer([EV["kicker2"]], "serif", 32, GOLD, tracking=15)
350
+ L_name = text_layer([EV["name"]], "script", 248, CREAM)
351
+ L_name_glow = alpha_scale(text_layer([EV["name"]], "script", 248, GOLD).filter(ImageFilter.GaussianBlur(16)), 0.55)
352
+ L_body2 = text_layer([EV["body2"]], "bodyi", 47, SOFT)
353
+ L_small2 = text_layer([EV["small2"]], "serif", 28, GOLD, tracking=10)
354
+ CARD_D = make_card(832, EV["card_title"], [
355
+ dict(label=EV["when_label"], values=[EV["when_1"], EV["when_2"]]),
356
+ dict(label=EV["where_label"], values=[EV["where_1"]]),
357
+ ])
358
+ L_sub3 = text_layer([EV["sub3"]], "bodyi", 42, SOFT)
359
+ MONO = build_monogram(600)
360
+ L_quote = text_layer(EV["quote"], "bodyi", 47, SOFT, line_h=76)
361
+ L_kick3 = text_layer(["THE CELEBRATION"], "serif", 32, GOLD, tracking=15)
362
+ L_script5 = text_layer([EV["script5"]], "script", 104, GOLD)
363
+ L_body5 = text_layer([EV["body5"]], "bodyi", 43, SOFT)
364
+ L_name5 = text_layer([EV["name"]], "script", 152, CREAM)
365
+ CARD_R = make_card(700, None, [dict(label=EV["rsvp_label"], values=[EV["rsvp_value"]])],
366
+ label_size=28, value_size=44)
367
+ L_kick6 = text_layer([EV["kicker6"]], "serif", 32, GOLD, tracking=15)
368
+ L_script6 = text_layer([EV["script6"]], "script", 136, GOLD)
369
+ L_sig = text_layer([EV["sig"]], "script", 80, CREAM)
370
+
371
+ # ----------------------------------------------------------------- timeline
372
+ DUR = [4.0, 5.0, 6.5, 5.5, 5.5, 5.5]
373
+ OV, HOLD = 0.55, 2.0
374
+ start = []
375
+ acc = 0.0
376
+ for d in DUR:
377
+ start.append(acc); acc += d - OV
378
+ LAST_END = start[-1] + DUR[-1]
379
+ TOTAL = LAST_END + HOLD
380
+ FADES = 0.75
381
+ NF = int(round(TOTAL * FPS))
382
+ print("timeline:", [round(s, 2) for s in start], "last end", round(LAST_END, 2), "total", round(TOTAL, 2), "frames", NF)
383
+
384
+ def scene_alpha(i, t):
385
+ s, e = start[i], (TOTAL if i == len(DUR) - 1 else start[i] + DUR[i])
386
+ if t < s or t > e:
387
+ return 0.0
388
+ if i + 1 < len(DUR) and t >= start[i + 1]:
389
+ return max(0.0, 1.0 - (t - start[i + 1]) / OV)
390
+ return 1.0
391
+
392
+ def ease(t, delay, dur=0.95):
393
+ x = (t - delay) / dur
394
+ if x <= 0:
395
+ return 0.0
396
+ if x >= 1:
397
+ return 1.0
398
+ return 1.0 - (1.0 - x) ** 3
399
+
400
+ # item = (layer, xf, yf, delay, dy, dur, anchor)
401
+ ITEMS = [
402
+ [(kicker1, 0.5, 0.278, 0.10, 40, 0.9, "c"),
403
+ (k1_extra, 0.5, 0.325, 0.28, 34, 0.9, "c"),
404
+ (L_script1, 0.5, 0.408, 0.48, 62, 1.05, "c"),
405
+ (LOTUS, 0.5, 0.497, 0.88, 18, 0.8, "c"),
406
+ (L_sub1, 0.5, 0.556, 1.05, 40, 0.9, "c")],
407
+
408
+ [(L_kick2, 0.5, 0.297, 0.10, 38, 0.9, "c"),
409
+ (L_name_glow, 0.5, 0.415, 0.42, 58, 1.1, "c"),
410
+ (L_name, 0.5, 0.415, 0.42, 58, 1.1, "c"),
411
+ (LOTUS, 0.5, 0.525, 0.88, 18, 0.8, "c"),
412
+ (L_body2, 0.5, 0.588, 1.00, 40, 0.9, "c"),
413
+ (L_small2, 0.5, 0.650, 1.20, 30, 0.9, "c")],
414
+
415
+ [(L_kick3, 0.5, 0.252, 0.10, 38, 0.9, "c"),
416
+ (CARD_D, 0.5, 0.488, 0.40, 56, 1.05, "c"),
417
+ (L_sub3, 0.5, 0.732, 1.05, 40, 0.9, "c")],
418
+
419
+ [(MONO, 0.5, 0.400, 0.20, 50, 1.05, "c"),
420
+ (LOTUS_S, 0.5, 0.548, 0.90, 18, 0.8, "c"),
421
+ (L_quote, 0.5, 0.648, 1.05, 44, 1.0, "c")],
422
+
423
+ [(L_script5, 0.5, 0.286, 0.15, 50, 1.0, "c"),
424
+ (L_body5, 0.5, 0.362, 0.52, 40, 0.9, "c"),
425
+ (L_name5, 0.5, 0.441, 0.80, 56, 1.05, "c"),
426
+ (CARD_R, 0.5, 0.615, 1.10, 50, 1.0, "c")],
427
+
428
+ [(L_kick6, 0.5, 0.336, 0.15, 36, 0.9, "c"),
429
+ (L_script6, 0.5, 0.428, 0.45, 58, 1.05, "c"),
430
+ (LOTUS, 0.5, 0.515, 0.88, 18, 0.8, "c"),
431
+ (L_sig, 0.5, 0.578, 1.08, 44, 0.95, "c")],
432
+ ]
433
+
434
+ def rgba_layer_to_box(lay):
435
+ return lay
436
+
437
+ def render(t, poster=False):
438
+ img = BG.copy()
439
+ mand = MANDALA.rotate((t * 7.5) % 360.0, resample=Image.BICUBIC)
440
+ img.alpha_composite(mand, (int(W / 2 - mand.size[0] / 2), int(H * 0.455 - mand.size[1] / 2)))
441
+ img.alpha_composite(TORAN, (0, 0))
442
+ img.alpha_composite(FRAME, (0, 0))
443
+ for ph_i, cx in enumerate((148, W - 148)):
444
+ fl = 0.68 + 0.24 * math.sin(t * 7.2 + ph_i * 1.9) + 0.10 * math.sin(t * 16.7 + ph_i * 2.4)
445
+ fl = max(0.30, min(1.0, fl))
446
+ g = GLOWS[int(fl * 11)]
447
+ cy = H - 168
448
+ img.alpha_composite(g, (int(cx - g.size[0] / 2), int(cy - g.size[1] / 2 - 46)))
449
+ img.alpha_composite(DIYA, (int(cx - DIYA.size[0] / 2), int(cy - DIYA.size[1] / 2 - 46)))
450
+ for m in MOTES:
451
+ yy = (m["y"] - m["sp"] * t) % (H + 240) - 120
452
+ xx = m["x"] + 26 * math.sin(t * m["sw"] + m["ph"])
453
+ al = 0.22 + 0.62 * max(0.0, math.sin(t * 0.85 + m["ph"]))
454
+ idx = max(0, min(11, int(al * 11)))
455
+ sp = MOTE_V[idx] if m["big"] else MOTE_S[idx]
456
+ px, py = int(xx - sp.size[0] / 2), int(yy - sp.size[1] / 2)
457
+ if -sp.size[0] < px < W and -sp.size[1] < py < H:
458
+ img.alpha_composite(sp, (max(0, px), max(0, py)) if (px >= 0 and py >= 0) else (px, py))
459
+ for i in range(len(DUR)):
460
+ sa = scene_alpha(i, t)
461
+ if sa <= 0.004:
462
+ continue
463
+ tl = t - start[i]
464
+ for (lay, xf, yf, delay, dy, dur, an) in ITEMS[i]:
465
+ a = ease(tl, delay, dur) * sa
466
+ if an == "c":
467
+ paste(img, lay, W * xf, H * yf, a, dy=(1 - ease(tl, delay, dur)) * dy)
468
+ else:
469
+ paste(img, lay, W * xf, H * yf, a, dy=(1 - ease(tl, delay, dur)) * dy, anchor="tl")
470
+ if poster:
471
+ return img.convert("RGB")
472
+ if t < FADES:
473
+ k = t / FADES
474
+ elif t > TOTAL - FADES:
475
+ k = max(0.0, (TOTAL - t) / FADES)
476
+ else:
477
+ k = 1.0
478
+ rgb = img.convert("RGB")
479
+ if k < 1.0:
480
+ arr = np.asarray(rgb, dtype=np.float32) * k
481
+ return Image.fromarray(arr.astype(np.uint8), "RGB")
482
+ return rgb
483
+
484
+ # ----------------------------------------------------------------- encode
485
+ from imageio_ffmpeg import get_ffmpeg_exe
486
+ FFMPEG = get_ffmpeg_exe()
487
+ print("ffmpeg:", FFMPEG)
488
+ cmd = [FFMPEG, "-y", "-loglevel", "error", "-f", "rawvideo", "-pix_fmt", "rgb24",
489
+ "-s", "%dx%d" % (W, H), "-r", str(FPS), "-i", "-",
490
+ "-c:v", "libx264", "-preset", "medium", "-crf", "20", "-pix_fmt", "yuv420p",
491
+ "-movflags", "+faststart", "-an", OUT]
492
+ proc = subprocess.Popen(cmd, stdin=subprocess.PIPE)
493
+
494
+ poster_t = start[1] + 2.6
495
+ for n in range(NF):
496
+ t = n / float(FPS)
497
+ fr = render(t, poster=(abs(t - poster_t) < 0.5 / FPS))
498
+ if abs(t - poster_t) < 0.5 / FPS:
499
+ fr.save(POSTER, quality=92)
500
+ proc.stdin.write(np.asarray(fr, dtype=np.uint8).tobytes())
501
+ if n % 120 == 0:
502
+ print("frame %4d / %d (t=%.1fs)" % (n, NF, t), flush=True)
503
+ proc.stdin.close()
504
+ proc.wait()
505
+ print("encoded:", OUT, os.path.getsize(OUT), "bytes")
506
+
507
+ # ----------------------------------------------------------------- upload
508
+ from huggingface_hub import HfApi
509
+ REPO = os.environ.get("REPO_ID", "coderdecoder01/retirement-invitation-mamta")
510
+ tok = os.environ.get("HF_TOKEN")
511
+ api = HfApi(token=tok)
512
+ api.create_repo(repo_id=REPO, repo_type="dataset", private=False, exist_ok=True)
513
+ api.upload_file(path_or_fileobj=OUT, path_in_repo="mamta-retirement-invitation.mp4",
514
+ repo_id=REPO, repo_type="dataset")
515
+ api.upload_file(path_or_fileobj=POSTER, path_in_repo="poster.jpg", repo_id=REPO, repo_type="dataset")
516
+ print("UPLOADED https://huggingface.co/datasets/%s/resolve/main/mamta-retirement-invitation.mp4" % REPO)
517
+ print("POSTER https://huggingface.co/datasets/%s/resolve/main/poster.jpg" % REPO)