File size: 1,532 Bytes
4ab78fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Render alignment animations without registration, cropping, or warping."""
from pathlib import Path
import re
from PIL import Image

ROOT = Path(__file__).resolve().parents[1]
samples = re.findall(r'src="assets/preview/PCB/([^\"]+)"', (ROOT / "README.md").read_text())
out = ROOT / "assets/preview/Overlay"
out.mkdir(parents=True, exist_ok=True)
for name in samples:
    with Image.open(ROOT / "assets/preview/PCB" / name) as image:
        pcb = image.convert("RGB")
    with Image.open(ROOT / "assets/preview/Layout" / name) as image:
        layout = image.convert("RGB")
    assert pcb.size == layout.size, f"Size mismatch: {name}"
    # Apply identical display downsampling; preserve the supplied correspondence.
    pcb.thumbnail((640, 640), Image.Resampling.LANCZOS)
    layout = layout.resize(pcb.size, Image.Resampling.LANCZOS)
    frames = [Image.blend(pcb, layout, alpha) for alpha in (0, 0.5, 1, 0.5)]
    # A shared palette prevents unrelated colors from flickering between frames.
    atlas = Image.new("RGB", (pcb.width * 3, pcb.height))
    for i, frame in enumerate(frames[:3]):
        atlas.paste(frame, (i * pcb.width, 0))
    palette = atlas.quantize(colors=256)
    frames = [frame.quantize(palette=palette, dither=Image.Dither.NONE) for frame in frames]
    frames[0].save(out / f"{Path(name).stem}.gif", save_all=True,
                   append_images=frames[1:], duration=[1000, 700, 1000, 700],
                   loop=0, disposal=2, optimize=False)
print(f"Rendered {len(samples)} animated overlays.")