"""'What is this called?' fill-in-the-blank variant: letter tiles like G _ _ / _ A Y, answer typed into the blanks. Usage: python render_blank.py [ids] (items3.json, photos in /tmp/work/user/.jpg)""" import json, math, os, sys sys.path.insert(0, os.path.dirname(__file__)) import render as R from render import (ACCENT, CONFETTI, F_NS, F_POP, F_POP_SB, F_SC, INK, YELLOW, Image, ImageDraw, clamp, ease_in_out, ease_out_back, ease_out_cubic, font, lerp, np, prog, text_w) TILE_W, TILE_H, GAP = 124, 144, 22 Y_TITLE, Y_TILES_Q, Y_TILES_A, Y_IPA, Y_MEAN = 162, 292, 196, 326, 410 class BlankClip(R.Clip): CHQ, CHA = 402, 462 pill_q, pill_a = '看图填空', '答案揭晓' GLASS = 0.50 # milky-white share of the frosted glass (was 0.70) — more see-through BLUR = 5 def __init__(self, item, *a, **k): self.hints = set(item['hints']) super().__init__(item, *a, **k) def typed_indices(self): return [i for i, c in enumerate(self.it['en']) if c != ' ' and i not in self.hints] def typing_dt(self, n): return clamp(0.36 / max(n, 1), 0.08, 0.18) def prepare_layout(self): super().prepare_layout() n = len(self.word) total = n * TILE_W + (n - 1) * GAP x0 = (self.CW - total) / 2 self.tiles_x = [x0 + i * (TILE_W + GAP) for i in range(n)] self.title = self.it.get('question', 'What is this called?') ts = 64 while text_w(self.title, font(F_POP, ts)) > self.CW - 120: ts -= 2 self.title_size = ts def content(self, t, fidx, ch): T = self.T CW = self.CW layer = Image.new('RGBA', (CW, ch), (0, 0, 0, 0)) d = ImageDraw.Draw(layer) self.draw_pill(layer, t) # question title hp = ease_in_out(prog(t, T['rev'], 0.22)) if hp < 1: d.text((CW / 2, Y_TITLE - 30 * hp), self.title, font=font(F_POP, self.title_size), fill=INK + (int(255 * (1 - hp)),), anchor='mm') # tiles sp = ease_in_out(prog(t, T['rev'], 0.35)) row_y = lerp(Y_TILES_Q, Y_TILES_A, sp) flash = ease_out_cubic(prog(t, T['done'] - 0.02, 0.25)) letters = self.word.upper() lsize = 100 for i, c in enumerate(letters): if c == ' ': continue x = self.tiles_x[i] wave = math.sin(clamp((t - T['done'] - i * 0.05) / 0.4) * math.pi) * 16 top = row_y - TILE_H / 2 - wave hint = i in self.hints # tile body: white -> highlighter yellow when the answer completes base_fill = (255, 255, 255, 205 if hint else 115) fill = tuple(int(lerp(base_fill[k], (YELLOW + (240,))[k], flash)) for k in range(4)) d.rounded_rectangle((x, top, x + TILE_W, top + TILE_H), 22, fill=fill, outline=(30, 30, 40, int(lerp(45, 0, flash))), width=3) if not hint: d.rounded_rectangle((x + 18, top + TILE_H - 16, x + TILE_W - 18, top + TILE_H - 8), 4, fill=INK + (int(lerp(220, 0, flash)),)) cx = x + TILE_W / 2 if hint: col = ACCENT if flash < 0.5 else INK d.text((cx, top + TILE_H / 2 + lsize * 0.36), c, font=font(F_POP, lsize), fill=col + (255,), anchor='ms') else: k = self.typed.index(i) appear = prog(t, T['type0'] + k * T['dt'], 0.16) if appear > 0: pop = ease_out_back(appear, 2.6) size = lsize * (0.45 + 0.55 * pop) base = top + TILE_H / 2 + size * 0.36 - 22 * (1 - ease_out_cubic(appear)) d.text((cx, base), c, font=font(F_POP, size), fill=INK + (int(255 * min(1, appear * 1.8)),), anchor='ms') # blinking caret in the first blank while the question is on screen if t < T['type0'] and self.typed and int(t * 2.4) % 2 == 0: x = self.tiles_x[self.typed[0]] + TILE_W / 2 d.rounded_rectangle((x - 3, row_y - 40, x + 3, row_y + 40), 3, fill=ACCENT + (230,)) # IPA row ip = ease_out_cubic(prog(t, T['ipa'], 0.32)) if ip > 0: fi = font(F_NS, 46, 500) ipa = self.it['ipa'] iw = text_w(ipa, fi) icon = 62 x0 = (CW - (icon + 20 + iw)) / 2 y = Y_IPA + 26 * (1 - ip) al = int(255 * ip) env = float(self.env[min(fidx, len(self.env) - 1)]) r = icon / 2 * (1 + 0.10 * env) icx = x0 + icon / 2 if env > 0.05: rr = icon / 2 + 6 + 16 * env d.ellipse((icx - rr, y - rr, icx + rr, y + rr), outline=ACCENT + (int(110 * env * ip),), width=3) d.ellipse((icx - r, y - r, icx + r, y + r), fill=ACCENT + (al,)) bx = icx - 13 d.polygon([(bx, y - 6), (bx + 7, y - 6), (bx + 16, y - 14), (bx + 16, y + 14), (bx + 7, y + 6), (bx, y + 6)], fill=(255, 255, 255, al)) for k2, rad in enumerate((9, 16)): aa = int(al * (0.55 + 0.45 * clamp(env * 2.2 - k2 * 0.5))) cxa = bx + 15 d.arc((cxa - rad, y - rad, cxa + rad, y + rad), -48, 48, fill=(255, 255, 255, aa), width=3) d.text((x0 + icon + 20, y), ipa, font=fi, fill=(40, 40, 52, al), anchor='lm') # meaning row mpn = ease_out_cubic(prog(t, T['mean'], 0.3)) if mpn > 0: al = int(255 * mpn) fp_ = font(F_POP_SB, 28) fz = font(F_SC, 38, 700) pos, zh = self.it['pos'], self.it['zh'] tagw = text_w(pos, fp_) + 26 x0 = (CW - (tagw + 16 + text_w(zh, fz))) / 2 y = Y_MEAN + 20 * (1 - mpn) d.rounded_rectangle((x0, y - 21, x0 + tagw, y + 21), 10, fill=(22, 22, 28, int(al * 0.9))) d.text((x0 + tagw / 2, y), pos, font=fp_, fill=(255, 255, 255, al), anchor='mm') d.text((x0 + tagw + 16, y), zh, font=fz, fill=(30, 30, 40, al), anchor='lm') # glossy light sweep swp = prog(t, T['sweep'], 0.55) if 0 < swp < 1: bx = int(lerp(-self.band.width, CW + 40, ease_in_out(swp))) gl = Image.new('L', (CW, ch), 0) gl.paste(self.band, (bx, -self.CHA // 2)) m = self.mask_for(CW, ch, 46) gl = Image.fromarray((np.asarray(gl, np.float32) * np.asarray(m, np.float32) / 255).astype(np.uint8)) white = Image.new('RGBA', (CW, ch), (255, 255, 255, 0)) white.putalpha(gl) layer = Image.alpha_composite(layer, white) return layer if __name__ == '__main__': items = json.load(open(os.path.join(os.path.dirname(__file__), os.environ.get('ITEMS', 'items3.json')))) want = sys.argv[1].split(',') if len(sys.argv) > 1 else [i['id'] for i in items] for k, it in enumerate(items, 1): if it['id'] not in want: continue c = BlankClip(it, k, len(items), f"/tmp/work/user/{it['id']}.jpg", f"/tmp/work/out/{it['id']}") c.snap = [int(x) for x in os.environ.get('SNAP', '').split(',') if x] print(it['id'], it['en'], 'frames', c.nf, 'dur', round(c.dur, 2), 'en2', c.T['en2'] is not None, flush=True) c.render()