coderdecoder01 commited on
Commit
8946a99
·
verified ·
1 Parent(s): 35e1036

Add Mamta's own message scene, teaching tribute, Kasauli venue, colour emoji

Browse files
Files changed (1) hide show
  1. render_invitation.py +170 -74
render_invitation.py CHANGED
@@ -1,7 +1,7 @@
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
@@ -21,32 +21,40 @@ SOFT = (244, 229, 203)
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", # shubh sevanivrutti
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
  GH = "https://raw.githubusercontent.com/google/fonts/main/ofl/"
 
 
 
 
50
  FONT_URLS = {
51
  "script": [GH + "greatvibes/GreatVibes-Regular.ttf"],
52
  "serif": [GH + "playfairdisplay/PlayfairDisplay%5Bwght%5D.ttf"],
@@ -54,45 +62,100 @@ FONT_URLS = {
54
  "bodyi": [GH + "cormorantgaramond/CormorantGaramond-Italic%5Bwght%5D.ttf"],
55
  "hindi": [GH + "notoserifdevanagari/NotoSerifDevanagari%5Bwdth%2Cwght%5D.ttf"],
56
  }
 
 
 
 
 
 
 
57
  FPATH = {}
58
  for key, urls in FONT_URLS.items():
59
  for i, u in enumerate(urls):
60
  dest = os.path.join(FDIR, "%s_%d.ttf" % (key, i))
61
  try:
62
- req = urllib.request.Request(u, headers={"User-Agent": "Mozilla/5.0"})
63
- with urllib.request.urlopen(req, timeout=90) as r, open(dest, "wb") as fh:
64
- fh.write(r.read())
65
  FPATH[key] = dest
66
- print("FONT OK %-7s %8d %s" % (key, os.path.getsize(dest), u.split("/")[-1]))
67
  break
68
  except Exception as e:
69
- print("FONT FAIL %-7s %s" % (key, repr(e)[:90]))
 
70
  HAS_RAQM = bool(features.check("raqm"))
71
  HAS_HINDI = HAS_RAQM and "hindi" in FPATH
72
- print("RAQM:", HAS_RAQM, "| Devanagari usable:", HAS_HINDI)
73
 
74
  _fc = {}
75
- def F(kind, size, weight=None):
76
- key = (kind, size, weight)
77
  if key in _fc:
78
  return _fc[key]
79
  path = FPATH.get(kind) or FPATH.get("serif") or FPATH.get("body")
80
  f = ImageFont.truetype(path, size)
81
- if weight and hasattr(f, "set_variation_by_name"):
82
- try:
83
- f.set_variation_by_name(str(weight))
84
- except Exception:
85
- try:
86
- f.set_variation_by_axes([weight] * len(f.get_variation_axes()))
87
- except Exception:
88
- pass
89
  _fc[key] = f
90
  return f
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  # ----------------------------------------------------------------- text helper
93
- def text_layer(lines, kind, size, color, tracking=0, line_h=None, weight=None):
94
- """Ink-cropped RGBA layer of centred text."""
95
- f = F(kind, size, weight)
96
  tmp = Image.new("RGBA", (8, 8)); td = ImageDraw.Draw(tmp)
97
  lh = line_h or int(size * 1.30)
98
  widths = []
@@ -117,12 +180,10 @@ def text_layer(lines, kind, size, color, tracking=0, line_h=None, weight=None):
117
  bb = lay.getbbox()
118
  return lay.crop(bb) if bb else lay
119
 
120
- def fit_layer(lines, kind, size, color, max_w, tracking=0, line_h=None, weight=None):
121
- """Shrink the point size until the rendered ink fits inside max_w pixels."""
122
- s = size
123
- lay = None
124
  while s > 18:
125
- lay = text_layer(lines, kind, s, color, tracking=tracking, line_h=line_h, weight=weight)
126
  if lay.size[0] <= max_w:
127
  return lay
128
  s = int(s * 0.94)
@@ -155,8 +216,7 @@ def paste(dst, lay, cx, cy, alpha, dy=0.0):
155
 
156
  def rule_strip(w, alpha, thickness=2):
157
  w = max(2, int(w))
158
- ramp = np.linspace(0.0, 1.0, w)
159
- a = (np.sin(np.pi * ramp) ** 1.4 * 255 * alpha).astype(np.uint8)
160
  st = np.zeros((thickness, w, 4), dtype=np.uint8)
161
  st[..., 0], st[..., 1], st[..., 2] = GOLD[0], GOLD[1], GOLD[2]
162
  st[..., 3] = a
@@ -200,8 +260,7 @@ def build_toran(height=210):
200
  x = x0 + (x1 - x0) * t
201
  yy = 52 + math.sin(math.pi * t) * 74
202
  r = 12.5
203
- col = MARIGOLD if i % 2 == 0 else ORANGE
204
- d.ellipse([x - r, yy - r, x + r, yy + r], fill=col + (238,))
205
  d.ellipse([x - r * 0.34, yy - r * 0.55, x + r * 0.18, yy - r * 0.12], fill=(255, 236, 196, 130))
206
  if k < 3:
207
  jx = x1
@@ -221,9 +280,8 @@ def build_mandala(size=1040):
221
  d.ellipse([c - r, c - r, c + r, c + r], outline=GOLD_L + (a,), width=int(wd * S))
222
  for i in range(24):
223
  ang = 360.0 * i / 24
224
- pw, ph = int(s * 0.115), int(s * 0.038)
225
- p = Image.new("RGBA", (pw, ph), (0, 0, 0, 0))
226
- ImageDraw.Draw(p).ellipse([0, 0, pw - 1, ph - 1], outline=GOLD_L + (85,), width=int(1.5 * S))
227
  p = p.rotate(-ang, expand=True, resample=Image.BICUBIC)
228
  r = s * 0.368 / 2.0
229
  im.alpha_composite(p, (int(c + r * math.cos(math.radians(ang)) - p.size[0] / 2),
@@ -268,9 +326,8 @@ def build_mote(size=26):
268
  def build_lotus(size=124):
269
  im = Image.new("RGBA", (size, size), (0, 0, 0, 0))
270
  for i in range(6):
271
- pw, ph = int(size * 0.86), int(size * 0.26)
272
- p = Image.new("RGBA", (pw, ph), (0, 0, 0, 0))
273
- ImageDraw.Draw(p).ellipse([0, 0, pw - 1, ph - 1], outline=GOLD_L + (215,), width=3)
274
  p = p.rotate(-(i * 30), expand=True, resample=Image.BICUBIC)
275
  im.alpha_composite(p, (size // 2 - p.size[0] // 2, size // 2 - p.size[1] // 2))
276
  ImageDraw.Draw(im).ellipse([size / 2 - 8, size / 2 - 8, size / 2 + 8, size / 2 + 8], fill=MARIGOLD + (235,))
@@ -294,14 +351,18 @@ def build_monogram(D=600):
294
  im.alpha_composite(lay, (int(c - lay.size[0] / 2), int(c - lay.size[1] / 2)))
295
  return im
296
 
297
- def make_card(width, title, sections, title_size=66, label_size=26, value_size=46, alpha=205):
298
- Hc = 1500
299
- card = Image.new("RGBA", (width, Hc), (0, 0, 0, 0))
300
  S = 2
301
- tmp = Image.new("RGBA", (width * S, Hc * S), (0, 0, 0, 0))
302
- ImageDraw.Draw(tmp).rounded_rectangle([0, 0, width * S - 1, Hc * S - 1], radius=int(16 * S),
303
- fill=(60, 11, 28, alpha), outline=GOLD + (235,), width=int(2.2 * S))
304
- card.alpha_composite(tmp.resize((width, Hc), Image.LANCZOS), (0, 0))
 
 
 
 
 
305
  y = 74
306
  if title:
307
  tl = fit_layer([title], "script", title_size, GOLD, width - 110)
@@ -327,8 +388,24 @@ def make_card(width, title, sections, title_size=66, label_size=26, value_size=4
327
  card.alpha_composite(build_lotus(64), (int(width / 2 - 32), int(used - 20)))
328
  return card.crop((0, 0, width, min(Hc, used + 54)))
329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
  # ----------------------------------------------------------------- build assets
331
- print("building assets ...")
332
  BG = build_bg().convert("RGBA")
333
  FRAME = build_frame()
334
  TORAN = build_toran()
@@ -345,7 +422,7 @@ MOTES = [dict(x=random.uniform(70, W - 70), y=random.uniform(0, H), sp=random.un
345
  sw=random.uniform(0.25, 0.7), ph=random.uniform(0, 6.28), big=random.random() < 0.45)
346
  for _ in range(16)]
347
 
348
- MAXW = 880 # keep clear of the gold frame
349
  kicker1 = text_layer([EV["hindi"]], "hindi", 46, GOLD) if HAS_HINDI else \
350
  text_layer([EV["kicker1_en"]], "serif", 34, GOLD, tracking=13)
351
  k1_extra = text_layer([EV["kicker1_en"]], "serif", 26, GOLD, tracking=11) if HAS_HINDI else None
@@ -357,25 +434,30 @@ L_name = fit_layer([EV["name"]], "script", 250, CREAM, MAXW)
357
  L_name_glow = alpha_scale(fit_layer([EV["name"]], "script", 250, GOLD, MAXW).filter(ImageFilter.GaussianBlur(16)), 0.55)
358
  L_body2 = fit_layer([EV["body2"]], "bodyi", 47, SOFT, MAXW)
359
  L_small2 = text_layer([EV["small2"]], "serif", 28, GOLD, tracking=10)
 
 
 
 
 
 
 
 
360
  CARD_D = make_card(832, EV["card_title"], [
361
- dict(label=EV["when_label"], values=[EV["when_1"], EV["when_2"]]),
 
362
  dict(label=EV["where_label"], values=[EV["where_1"]]),
363
  ])
364
  L_sub3 = fit_layer([EV["sub3"]], "bodyi", 42, SOFT, MAXW)
 
365
  MONO = build_monogram(600)
366
  L_quote = fit_layer(EV["quote"], "bodyi", 47, SOFT, MAXW - 60, line_h=76)
367
- L_kick3 = text_layer(["THE CELEBRATION"], "serif", 32, GOLD, tracking=15)
368
- L_script5 = fit_layer([EV["script5"]], "script", 104, GOLD, MAXW)
369
- L_body5 = fit_layer([EV["body5"]], "bodyi", 43, SOFT, MAXW)
370
- L_name5 = fit_layer([EV["name"]], "script", 152, CREAM, MAXW)
371
- CARD_R = make_card(700, None, [dict(label=EV["rsvp_label"], values=[EV["rsvp_value"]])],
372
- label_size=28, value_size=44)
373
- L_kick6 = text_layer([EV["kicker6"]], "serif", 32, GOLD, tracking=15)
374
  L_script6 = fit_layer([EV["script6"]], "script", 136, GOLD, MAXW)
375
  L_sig = fit_layer([EV["sig"]], "script", 80, CREAM, MAXW)
 
376
 
377
  # ----------------------------------------------------------------- timeline
378
- DUR = [4.0, 5.0, 6.5, 5.5, 5.5, 5.5]
379
  OV, HOLD, FADES = 0.55, 2.0, 0.75
380
  start, acc = [], 0.0
381
  for d in DUR:
@@ -383,7 +465,7 @@ for d in DUR:
383
  LAST_END = start[-1] + DUR[-1]
384
  TOTAL = LAST_END + HOLD
385
  NF = int(round(TOTAL * FPS))
386
- print("timeline start:", [round(s, 2) for s in start], "total %.2fs %d frames" % (TOTAL, NF))
387
 
388
  def scene_alpha(i, t):
389
  s = start[i]
@@ -408,12 +490,12 @@ ITEMS = [
408
  (L_sub1, 0.556, 1.05, 40, 0.9)],
409
  [(L_kick2, 0.297, 0.10, 38, 0.9), (L_name_glow, 0.415, 0.42, 58, 1.1), (L_name, 0.415, 0.42, 58, 1.1),
410
  (LOTUS, 0.525, 0.88, 18, 0.8), (L_body2, 0.588, 1.00, 40, 0.9), (L_small2, 0.650, 1.20, 30, 0.9)],
411
- [(L_kick3, 0.252, 0.10, 38, 0.9), (CARD_D, 0.488, 0.40, 56, 1.05), (L_sub3, 0.732, 1.05, 40, 0.9)],
412
- [(MONO, 0.400, 0.20, 50, 1.05), (LOTUS_S, 0.548, 0.90, 18, 0.8), (L_quote, 0.648, 1.05, 44, 1.0)],
413
- [(L_script5, 0.286, 0.15, 50, 1.0), (L_body5, 0.362, 0.52, 40, 0.9), (L_name5, 0.441, 0.80, 56, 1.05),
414
- (CARD_R, 0.615, 1.10, 50, 1.0)],
415
- [(L_kick6, 0.336, 0.15, 36, 0.9), (L_script6, 0.428, 0.45, 58, 1.05), (LOTUS, 0.515, 0.88, 18, 0.8),
416
- (L_sig, 0.578, 1.08, 44, 0.95)],
417
  ]
418
 
419
  def render(t):
@@ -434,7 +516,7 @@ def render(t):
434
  al = 0.22 + 0.62 * max(0.0, math.sin(t * 0.85 + m["ph"]))
435
  sp = (MOTE_V if m["big"] else MOTE_S)[max(0, min(11, int(al * 11)))]
436
  px, py = int(xx - sp.size[0] / 2), int(yy - sp.size[1] / 2)
437
- if px >= 0 and py >= 0 and px < W and py < H:
438
  img.alpha_composite(sp, (px, py))
439
  for i in range(len(DUR)):
440
  sa = scene_alpha(i, t)
@@ -459,7 +541,7 @@ def render(t):
459
  # ----------------------------------------------------------------- encode
460
  from imageio_ffmpeg import get_ffmpeg_exe
461
  FFMPEG = get_ffmpeg_exe()
462
- print("ffmpeg:", FFMPEG)
463
  proc = subprocess.Popen([FFMPEG, "-y", "-loglevel", "error", "-f", "rawvideo", "-pix_fmt", "rgb24",
464
  "-s", "%dx%d" % (W, H), "-r", str(FPS), "-i", "-",
465
  "-c:v", "libx264", "-preset", "medium", "-crf", "20", "-pix_fmt", "yuv420p",
@@ -476,7 +558,19 @@ for n in range(NF):
476
  print("frame %4d / %d (t=%.1fs)" % (n, NF, t), flush=True)
477
  proc.stdin.close()
478
  proc.wait()
479
- print("encoded:", OUT, os.path.getsize(OUT), "bytes")
 
 
 
 
 
 
 
 
 
 
 
 
480
 
481
  # ----------------------------------------------------------------- upload
482
  from huggingface_hub import HfApi
@@ -485,5 +579,7 @@ api = HfApi(token=os.environ.get("HF_TOKEN"))
485
  api.create_repo(repo_id=REPO, repo_type="dataset", private=False, exist_ok=True)
486
  api.upload_file(path_or_fileobj=OUT, path_in_repo="mamta-retirement-invitation.mp4", repo_id=REPO, repo_type="dataset")
487
  api.upload_file(path_or_fileobj=POSTER, path_in_repo="poster.jpg", repo_id=REPO, repo_type="dataset")
 
 
488
  print("VIDEO https://huggingface.co/datasets/%s/resolve/main/mamta-retirement-invitation.mp4" % REPO)
489
  print("POSTER https://huggingface.co/datasets/%s/resolve/main/poster.jpg" % REPO)
 
1
  #!/usr/bin/env python3
2
  """
3
+ Indian-style animated retirement invitation for Mamta: 1080x1920, 30fps, H.264 MP4.
4
+ Maroon/gold palette, rotating mandala, marigold toran, flickering diyas, colour emoji.
5
  """
6
  import os, math, subprocess, urllib.request, random
7
  import numpy as np
 
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
+ # --- her own words ------------------------------------------------
32
+ message=[
33
+ dict(size=47, color=CREAM, lines=["\u201cThe bell is ringing one last time\u2026\u201d"]),
34
+ dict(size=42, color=SOFT, lines=["It is now time to say goodbye", "to my teaching profession."]),
35
+ dict(size=42, color=SOFT, lines=["To seek your good wishes & blessings,", "a party has been organised."]),
36
+ ],
37
+ msg_sig="\u2014 Mamta",
38
  card_title="Celebratory Lunch",
39
  when_label="WHEN",
40
  when_1="Wednesday, 30 September 2026",
41
+ time_label="TIME",
42
+ time_1="13.30 hrs (1:30 pm)",
43
  where_label="WHERE",
44
+ where_1="New Ros Common, Kasauli",
45
  sub3="Your presence will make it complete",
46
+ quote=["The bell has rung for the last time,", "but your lessons will echo on."],
47
+ kicker6="WITH LOVE AND BLESSINGS",
 
 
 
 
48
  script6="Celebrate With You",
49
+ sig="Mamta & family",
50
  )
51
 
52
  # ----------------------------------------------------------------- fonts
53
  GH = "https://raw.githubusercontent.com/google/fonts/main/ofl/"
54
+ EMOJI_URLS = [
55
+ "https://raw.githubusercontent.com/googlefonts/noto-emoji/main/fonts/NotoColorEmoji.ttf",
56
+ "https://raw.githubusercontent.com/googlefonts/noto-emoji/main/fonts/Noto-COLRv1-emojicompat.ttf",
57
+ ]
58
  FONT_URLS = {
59
  "script": [GH + "greatvibes/GreatVibes-Regular.ttf"],
60
  "serif": [GH + "playfairdisplay/PlayfairDisplay%5Bwght%5D.ttf"],
 
62
  "bodyi": [GH + "cormorantgaramond/CormorantGaramond-Italic%5Bwght%5D.ttf"],
63
  "hindi": [GH + "notoserifdevanagari/NotoSerifDevanagari%5Bwdth%2Cwght%5D.ttf"],
64
  }
65
+
66
+ def fetch(url, dest):
67
+ req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
68
+ with urllib.request.urlopen(req, timeout=120) as r, open(dest, "wb") as fh:
69
+ fh.write(r.read())
70
+ return os.path.getsize(dest)
71
+
72
  FPATH = {}
73
  for key, urls in FONT_URLS.items():
74
  for i, u in enumerate(urls):
75
  dest = os.path.join(FDIR, "%s_%d.ttf" % (key, i))
76
  try:
77
+ print("FONT OK %-7s %8d %s" % (key, fetch(u, dest), u.split("/")[-1]), flush=True)
 
 
78
  FPATH[key] = dest
 
79
  break
80
  except Exception as e:
81
+ print("FONT FAIL %-7s %s" % (key, repr(e)[:90]), flush=True)
82
+
83
  HAS_RAQM = bool(features.check("raqm"))
84
  HAS_HINDI = HAS_RAQM and "hindi" in FPATH
85
+ print("RAQM:", HAS_RAQM, "| Devanagari usable:", HAS_HINDI, flush=True)
86
 
87
  _fc = {}
88
+ def F(kind, size):
89
+ key = (kind, size)
90
  if key in _fc:
91
  return _fc[key]
92
  path = FPATH.get(kind) or FPATH.get("serif") or FPATH.get("body")
93
  f = ImageFont.truetype(path, size)
 
 
 
 
 
 
 
 
94
  _fc[key] = f
95
  return f
96
 
97
+ # ----------------------------------------------------------------- colour emoji
98
+ EMOJI_PATH, EMOJI_SIZE = None, 137
99
+ for i, u in enumerate(EMOJI_URLS):
100
+ dest = os.path.join(FDIR, "emoji_%d.ttf" % i)
101
+ try:
102
+ sz = fetch(u, dest)
103
+ except Exception as e:
104
+ print("EMOJI FAIL download %s" % repr(e)[:80], flush=True)
105
+ continue
106
+ for size in (137, 109, 128, 96, 64, 160, 48):
107
+ try:
108
+ f = ImageFont.truetype(dest, size)
109
+ probe = Image.new("RGBA", (size * 2, size * 2), (0, 0, 0, 0))
110
+ ImageDraw.Draw(probe).text((size, size), "\U0001F339", font=f,
111
+ embedded_color=True, anchor="mm")
112
+ bb = probe.getbbox()
113
+ cov = 0.0
114
+ if bb:
115
+ arr = np.array(probe.crop(bb))
116
+ cov = float((arr[..., 3] > 30).mean())
117
+ if bb and cov > 0.25:
118
+ EMOJI_PATH, EMOJI_SIZE = dest, size
119
+ print("EMOJI OK %s size=%d coverage=%.2f" % (u.split("/")[-1], size, cov), flush=True)
120
+ break
121
+ except Exception as e:
122
+ print("EMOJI size %d failed: %s" % (size, repr(e)[:70]), flush=True)
123
+ if EMOJI_PATH:
124
+ break
125
+
126
+ def emoji_layer(chars, height, spacing=16):
127
+ if EMOJI_PATH is None:
128
+ return None
129
+ try:
130
+ f = ImageFont.truetype(EMOJI_PATH, EMOJI_SIZE)
131
+ except Exception:
132
+ return None
133
+ imgs = []
134
+ for ch in chars:
135
+ canvas = Image.new("RGBA", (EMOJI_SIZE * 2, EMOJI_SIZE * 2), (0, 0, 0, 0))
136
+ try:
137
+ ImageDraw.Draw(canvas).text((EMOJI_SIZE, EMOJI_SIZE), ch, font=f,
138
+ embedded_color=True, anchor="mm")
139
+ except Exception:
140
+ return None
141
+ bb = canvas.getbbox()
142
+ if not bb:
143
+ return None
144
+ imgs.append(canvas.crop(bb))
145
+ out = []
146
+ for g in imgs:
147
+ s = height / float(g.size[1])
148
+ out.append(g.resize((max(1, int(g.size[0] * s)), height), Image.LANCZOS))
149
+ tw = sum(g.size[0] for g in out) + spacing * (len(out) - 1)
150
+ lay = Image.new("RGBA", (tw, height), (0, 0, 0, 0))
151
+ x = 0
152
+ for g in out:
153
+ lay.alpha_composite(g, (x, 0)); x += g.size[0] + spacing
154
+ return lay
155
+
156
  # ----------------------------------------------------------------- text helper
157
+ def text_layer(lines, kind, size, color, tracking=0, line_h=None):
158
+ f = F(kind, size)
 
159
  tmp = Image.new("RGBA", (8, 8)); td = ImageDraw.Draw(tmp)
160
  lh = line_h or int(size * 1.30)
161
  widths = []
 
180
  bb = lay.getbbox()
181
  return lay.crop(bb) if bb else lay
182
 
183
+ def fit_layer(lines, kind, size, color, max_w, tracking=0, line_h=None):
184
+ s, lay = size, None
 
 
185
  while s > 18:
186
+ lay = text_layer(lines, kind, s, color, tracking=tracking, line_h=line_h)
187
  if lay.size[0] <= max_w:
188
  return lay
189
  s = int(s * 0.94)
 
216
 
217
  def rule_strip(w, alpha, thickness=2):
218
  w = max(2, int(w))
219
+ a = (np.sin(np.pi * np.linspace(0.0, 1.0, w)) ** 1.4 * 255 * alpha).astype(np.uint8)
 
220
  st = np.zeros((thickness, w, 4), dtype=np.uint8)
221
  st[..., 0], st[..., 1], st[..., 2] = GOLD[0], GOLD[1], GOLD[2]
222
  st[..., 3] = a
 
260
  x = x0 + (x1 - x0) * t
261
  yy = 52 + math.sin(math.pi * t) * 74
262
  r = 12.5
263
+ d.ellipse([x - r, yy - r, x + r, yy + r], fill=(MARIGOLD if i % 2 == 0 else ORANGE) + (238,))
 
264
  d.ellipse([x - r * 0.34, yy - r * 0.55, x + r * 0.18, yy - r * 0.12], fill=(255, 236, 196, 130))
265
  if k < 3:
266
  jx = x1
 
280
  d.ellipse([c - r, c - r, c + r, c + r], outline=GOLD_L + (a,), width=int(wd * S))
281
  for i in range(24):
282
  ang = 360.0 * i / 24
283
+ p = Image.new("RGBA", (int(s * 0.115), int(s * 0.038)), (0, 0, 0, 0))
284
+ ImageDraw.Draw(p).ellipse([0, 0, p.size[0] - 1, p.size[1] - 1], outline=GOLD_L + (85,), width=int(1.5 * S))
 
285
  p = p.rotate(-ang, expand=True, resample=Image.BICUBIC)
286
  r = s * 0.368 / 2.0
287
  im.alpha_composite(p, (int(c + r * math.cos(math.radians(ang)) - p.size[0] / 2),
 
326
  def build_lotus(size=124):
327
  im = Image.new("RGBA", (size, size), (0, 0, 0, 0))
328
  for i in range(6):
329
+ p = Image.new("RGBA", (int(size * 0.86), int(size * 0.26)), (0, 0, 0, 0))
330
+ ImageDraw.Draw(p).ellipse([0, 0, p.size[0] - 1, p.size[1] - 1], outline=GOLD_L + (215,), width=3)
 
331
  p = p.rotate(-(i * 30), expand=True, resample=Image.BICUBIC)
332
  im.alpha_composite(p, (size // 2 - p.size[0] // 2, size // 2 - p.size[1] // 2))
333
  ImageDraw.Draw(im).ellipse([size / 2 - 8, size / 2 - 8, size / 2 + 8, size / 2 + 8], fill=MARIGOLD + (235,))
 
351
  im.alpha_composite(lay, (int(c - lay.size[0] / 2), int(c - lay.size[1] / 2)))
352
  return im
353
 
354
+ def card_base(width, height, radius=16, fill=(60, 11, 28, 205)):
355
+ card = Image.new("RGBA", (width, height), (0, 0, 0, 0))
 
356
  S = 2
357
+ tmp = Image.new("RGBA", (width * S, height * S), (0, 0, 0, 0))
358
+ ImageDraw.Draw(tmp).rounded_rectangle([0, 0, width * S - 1, height * S - 1], radius=int(radius * S),
359
+ fill=fill, outline=GOLD + (235,), width=int(2.2 * S))
360
+ card.alpha_composite(tmp.resize((width, height), Image.LANCZOS), (0, 0))
361
+ return card
362
+
363
+ def make_card(width, title, sections, title_size=66, label_size=26, value_size=46):
364
+ Hc = 1600
365
+ card = card_base(width, Hc)
366
  y = 74
367
  if title:
368
  tl = fit_layer([title], "script", title_size, GOLD, width - 110)
 
388
  card.alpha_composite(build_lotus(64), (int(width / 2 - 32), int(used - 20)))
389
  return card.crop((0, 0, width, min(Hc, used + 54)))
390
 
391
+ def make_message_card(width=880, sig=None, sig_size=58):
392
+ Hc = 1600
393
+ card = card_base(width, Hc)
394
+ y = 86
395
+ paras = EV["message"]
396
+ for pi, p in enumerate(paras):
397
+ lay = fit_layer(p["lines"], "bodyi", p["size"], p["color"], width - 110,
398
+ line_h=int(p["size"] * 1.34))
399
+ card.alpha_composite(lay, (int(width / 2 - lay.size[0] / 2), int(y)))
400
+ y += lay.size[1] + (46 if pi < len(paras) - 1 else 30)
401
+ if sig:
402
+ sl = fit_layer([sig], "script", sig_size, GOLD, width - 160)
403
+ card.alpha_composite(sl, (int(width / 2 - sl.size[0] / 2), int(y)))
404
+ y += sl.size[1]
405
+ return card.crop((0, 0, width, min(Hc, y + 74)))
406
+
407
  # ----------------------------------------------------------------- build assets
408
+ print("building assets ...", flush=True)
409
  BG = build_bg().convert("RGBA")
410
  FRAME = build_frame()
411
  TORAN = build_toran()
 
422
  sw=random.uniform(0.25, 0.7), ph=random.uniform(0, 6.28), big=random.random() < 0.45)
423
  for _ in range(16)]
424
 
425
+ MAXW = 880
426
  kicker1 = text_layer([EV["hindi"]], "hindi", 46, GOLD) if HAS_HINDI else \
427
  text_layer([EV["kicker1_en"]], "serif", 34, GOLD, tracking=13)
428
  k1_extra = text_layer([EV["kicker1_en"]], "serif", 26, GOLD, tracking=11) if HAS_HINDI else None
 
434
  L_name_glow = alpha_scale(fit_layer([EV["name"]], "script", 250, GOLD, MAXW).filter(ImageFilter.GaussianBlur(16)), 0.55)
435
  L_body2 = fit_layer([EV["body2"]], "bodyi", 47, SOFT, MAXW)
436
  L_small2 = text_layer([EV["small2"]], "serif", 28, GOLD, tracking=10)
437
+
438
+ EMOJI_ROSE = emoji_layer(["\U0001F339"], 62) # rose
439
+ EMOJI_ROW = emoji_layer(["\U0001F339", "\U0001F64F", "\U0001F389", "\U0001F38A"], 62) # rose, pray, party, confetti
440
+ if EMOJI_ROW is None:
441
+ print("emoji unavailable - falling back to drawn ornaments", flush=True)
442
+ EMOJI_ROW = build_lotus(62)
443
+
444
+ CARD_MSG = make_message_card(sig=EV["msg_sig"])
445
  CARD_D = make_card(832, EV["card_title"], [
446
+ dict(label=EV["when_label"], values=[EV["when_1"]]),
447
+ dict(label=EV["time_label"], values=[EV["time_1"]]),
448
  dict(label=EV["where_label"], values=[EV["where_1"]]),
449
  ])
450
  L_sub3 = fit_layer([EV["sub3"]], "bodyi", 42, SOFT, MAXW)
451
+ L_kick3 = text_layer(["THE CELEBRATION"], "serif", 32, GOLD, tracking=15)
452
  MONO = build_monogram(600)
453
  L_quote = fit_layer(EV["quote"], "bodyi", 47, SOFT, MAXW - 60, line_h=76)
454
+ L_kick6 = text_layer([EV["kicker6"]], "serif", 30, GOLD, tracking=13)
 
 
 
 
 
 
455
  L_script6 = fit_layer([EV["script6"]], "script", 136, GOLD, MAXW)
456
  L_sig = fit_layer([EV["sig"]], "script", 80, CREAM, MAXW)
457
+ print("card sizes: message=%s details=%s" % (CARD_MSG.size, CARD_D.size), flush=True)
458
 
459
  # ----------------------------------------------------------------- timeline
460
+ DUR = [4.5, 5.0, 8.0, 7.0, 5.0, 5.5]
461
  OV, HOLD, FADES = 0.55, 2.0, 0.75
462
  start, acc = [], 0.0
463
  for d in DUR:
 
465
  LAST_END = start[-1] + DUR[-1]
466
  TOTAL = LAST_END + HOLD
467
  NF = int(round(TOTAL * FPS))
468
+ print("timeline start:", [round(s, 2) for s in start], "total %.2fs %d frames" % (TOTAL, NF), flush=True)
469
 
470
  def scene_alpha(i, t):
471
  s = start[i]
 
490
  (L_sub1, 0.556, 1.05, 40, 0.9)],
491
  [(L_kick2, 0.297, 0.10, 38, 0.9), (L_name_glow, 0.415, 0.42, 58, 1.1), (L_name, 0.415, 0.42, 58, 1.1),
492
  (LOTUS, 0.525, 0.88, 18, 0.8), (L_body2, 0.588, 1.00, 40, 0.9), (L_small2, 0.650, 1.20, 30, 0.9)],
493
+ [(EMOJI_ROSE, 0.168, 0.15, 30, 0.8), (CARD_MSG, 0.500, 0.45, 54, 1.05),
494
+ (L_kick3, 0.815, 1.10, 34, 0.9)],
495
+ [(L_kick3, 0.252, 0.10, 38, 0.9), (CARD_D, 0.500, 0.40, 56, 1.05), (L_sub3, 0.748, 1.05, 40, 0.9)],
496
+ [(MONO, 0.395, 0.20, 50, 1.05), (LOTUS_S, 0.545, 0.90, 18, 0.8), (L_quote, 0.645, 1.05, 44, 1.0)],
497
+ [(L_kick6, 0.330, 0.15, 36, 0.9), (EMOJI_ROW, 0.392, 0.45, 34, 0.85),
498
+ (L_script6, 0.468, 0.65, 58, 1.05), (LOTUS, 0.545, 0.95, 18, 0.8), (L_sig, 0.612, 1.10, 44, 0.95)],
499
  ]
500
 
501
  def render(t):
 
516
  al = 0.22 + 0.62 * max(0.0, math.sin(t * 0.85 + m["ph"]))
517
  sp = (MOTE_V if m["big"] else MOTE_S)[max(0, min(11, int(al * 11)))]
518
  px, py = int(xx - sp.size[0] / 2), int(yy - sp.size[1] / 2)
519
+ if 0 <= px < W and 0 <= py < H:
520
  img.alpha_composite(sp, (px, py))
521
  for i in range(len(DUR)):
522
  sa = scene_alpha(i, t)
 
541
  # ----------------------------------------------------------------- encode
542
  from imageio_ffmpeg import get_ffmpeg_exe
543
  FFMPEG = get_ffmpeg_exe()
544
+ print("ffmpeg:", FFMPEG, flush=True)
545
  proc = subprocess.Popen([FFMPEG, "-y", "-loglevel", "error", "-f", "rawvideo", "-pix_fmt", "rgb24",
546
  "-s", "%dx%d" % (W, H), "-r", str(FPS), "-i", "-",
547
  "-c:v", "libx264", "-preset", "medium", "-crf", "20", "-pix_fmt", "yuv420p",
 
558
  print("frame %4d / %d (t=%.1fs)" % (n, NF, t), flush=True)
559
  proc.stdin.close()
560
  proc.wait()
561
+ print("encoded:", OUT, os.path.getsize(OUT), "bytes", flush=True)
562
+
563
+ # preview stills for quality control
564
+ PREVIEW_DIR = "/tmp/previews"
565
+ os.makedirs(PREVIEW_DIR, exist_ok=True)
566
+ checks = [("s1", start[0] + 3.4), ("s2", start[1] + 3.2), ("s3", start[2] + 5.5),
567
+ ("s4", start[3] + 5.2), ("s5", start[4] + 3.0), ("s6", start[5] + 3.9)]
568
+ previews = []
569
+ for name, tt in checks:
570
+ fp = os.path.join(PREVIEW_DIR, "preview_%s.jpg" % name)
571
+ render(tt).save(fp, quality=88)
572
+ previews.append(fp)
573
+ print("preview", name, "t=%.2f" % tt, flush=True)
574
 
575
  # ----------------------------------------------------------------- upload
576
  from huggingface_hub import HfApi
 
579
  api.create_repo(repo_id=REPO, repo_type="dataset", private=False, exist_ok=True)
580
  api.upload_file(path_or_fileobj=OUT, path_in_repo="mamta-retirement-invitation.mp4", repo_id=REPO, repo_type="dataset")
581
  api.upload_file(path_or_fileobj=POSTER, path_in_repo="poster.jpg", repo_id=REPO, repo_type="dataset")
582
+ for fp in previews:
583
+ api.upload_file(path_or_fileobj=fp, path_in_repo="previews/" + os.path.basename(fp), repo_id=REPO, repo_type="dataset")
584
  print("VIDEO https://huggingface.co/datasets/%s/resolve/main/mamta-retirement-invitation.mp4" % REPO)
585
  print("POSTER https://huggingface.co/datasets/%s/resolve/main/poster.jpg" % REPO)