Overview
Concrete tool for rendering animated GIF flow diagrams from a JSON specification using Python and Pillow, provided by the visual-flow-gif skill.
Description
The render_animated_gif.py script reads a JSON diagram spec and renders both a static PNG and an animated GIF using Pillow. The FlowGifRenderer class handles theme loading (light/dark presets with overrides), node drawing (box, diamond, note, label types), edge routing with rounded corners and arrowheads, and animation overlays (moving glow particles along edges + pulsing node halos). Renders at 2x supersampling then downscales via LANCZOS for quality. Includes built-in verification: gif_motion_report() samples frame diffs, and validate_outputs() checks file existence, dimensions, frame count, and motion presence.
Usage
Run this script after authoring a JSON spec (see visual_flow_gif_skill for the spec format). Always use --verify --check before delivery to confirm the GIF has visible motion and correct dimensions.
Code Reference
Source Location
Signature
class FlowGifRenderer:
def __init__(self, spec: dict):
"""Initialize renderer from a JSON spec dict.
Loads canvas config, theme, and node index.
"""
def write(self, outdir: Path, basename: str = "flow-diagram") -> dict:
"""Render static PNG + animated GIF to outdir.
Returns: {"png": str, "gif": str} — paths to generated files.
"""
def draw_scene(self) -> Image:
"""Draw the full static diagram (header, nodes, edges)."""
def frame(self, base: Image, index: int, total: int) -> Image:
"""Render a single animation frame with motion overlay."""
def finish_static(self, image: Image) -> Image:
"""Apply film grain + vignette post-processing."""
def gif_motion_report(gif_path: str) -> dict:
"""Sample 4 frames and compute per-pair pixel diffs.
Returns: {"frames": int, "diffs": [{"from", "to", "changed_pixels"}]}
"""
def validate_outputs(result: dict, spec: dict) -> dict:
"""Validate that output files exist and match spec dimensions.
Checks: png_exists, gif_exists, png_width, png_height,
gif_width, gif_height, gif_frames, gif_has_motion.
Returns: {"ok": bool, "checks": list}
"""
def main():
"""CLI entry point.
Args (via argparse):
--spec: str (required) Path to JSON diagram spec
--outdir: str (required) Output directory
--basename: str (default: "flow-diagram") Filename prefix
--verify: flag Print sampled GIF frame differences
--check: flag Validate output files, exit(1) on failure
"""
Import
# Run as CLI script:
# python render_animated_gif.py --spec spec.json --outdir ./output --verify --check
# Or import the renderer class:
from render_animated_gif import FlowGifRenderer, gif_motion_report, validate_outputs
renderer = FlowGifRenderer(spec_dict)
result = renderer.write(Path("./output"), "diagram")
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| --spec |
str (file path) |
Yes |
Path to JSON diagram spec
|
| --outdir |
str (dir path) |
Yes |
Output directory for PNG and GIF
|
| --basename |
str |
No |
Filename prefix (default: flow-diagram)
|
| --verify |
flag |
No |
Print sampled frame differences to stdout
|
| --check |
flag |
No |
Validate outputs; exit(1) if any check fails
|
Outputs
| Name |
Type |
Description
|
| {basename}.png |
PNG image |
Static diagram (1280x800 default), downscaled from 2x supersampled render
|
| {basename}.gif |
GIF image |
Animated diagram (48 frames at 20fps default), with motion particles and pulse halos
|
| stdout |
JSON |
Result dict with file paths; verification and/or checks if flags set
|
Key Constants
| Constant |
Value |
Description
|
| DEFAULT_CANVAS |
1280x800, 20fps, 48 frames |
Default canvas dimensions and animation
|
| RENDER_SCALE |
2 |
Internal 2x supersampling factor
|
| THEMES |
light, dark |
Color palette presets with 17 color keys each
|
Usage Examples
Basic Render with Verification
# Render diagram and verify output
python render_animated_gif.py \
--spec spec.json \
--outdir ./output \
--basename flow-diagram \
--verify \
--check
# Output (stdout):
# {
# "png": "./output/flow-diagram.png",
# "gif": "./output/flow-diagram.gif",
# "verification": {"frames": 48, "diffs": [...]},
# "checks": {"ok": true, "checks": [...]}
# }
Programmatic Usage
import json
from pathlib import Path
from render_animated_gif import FlowGifRenderer
spec = json.loads(Path("spec.json").read_text())
renderer = FlowGifRenderer(spec)
result = renderer.write(Path("./output"), "diagram")
print(f"PNG: {result['png']}")
print(f"GIF: {result['gif']}")
Verify Motion Only
from render_animated_gif import gif_motion_report
report = gif_motion_report("./output/diagram.gif")
print(f"Frames: {report['frames']}")
for d in report["diffs"]:
print(f" {d['from']}→{d['to']}: {d['changed_pixels']} changed pixels")
Related Pages
Implements Principle
Requires Environment
Uses Heuristic