#!/usr/bin/env python3
"""Embed parts (webp) + parts.json into bob.tpl.html -> bob.html (works from file://).
Run after cut.py: /cache/tmp/venv-anim/bin/python build.py"""
import base64, io, json, os
from PIL import Image
HERE = os.path.dirname(os.path.abspath(__file__))
meta = json.load(open(f'{HERE}/parts.json'))
imgs = {}
for f in sorted(os.listdir(f'{HERE}/parts')):
    if not f.endswith('.png'): continue
    b = io.BytesIO(); Image.open(f'{HERE}/parts/{f}').save(b, 'WEBP', quality=90, method=6)
    imgs[f[:-4]] = 'data:image/webp;base64,' + base64.b64encode(b.getvalue()).decode()
html = open(f'{HERE}/bob.tpl.html').read()
html = html.replace('/*PARTS*/null', json.dumps(meta)).replace('/*IMGS*/null', json.dumps(imgs))
open(f'{HERE}/bob.html', 'w').write(html)
print('bob.html', len(html) // 1024, 'KB')
