Shenrui-Ma/video-regen-recipes / templates/drawing-process/svg-redraw-comparison
svg-redraw-comparison
Unverified用参考图制作高精度 SVG 临摹绘制过程视频,交付纯 SVG 网页、独立 SVG 和原图对照 MP4。禁止 Canvas、位图嵌入与外链,按构图、线稿、固有色、阴影、高光、细节重放;横图上下、竖图左右合成。
| File | Kind | Size |
|---|---|---|
| LICENSE | other | 1.1 KB |
| LICENSES.md | markdown | 998 B |
| README.md | markdown | 2.5 KB |
| SKILL.md | skill md | 3.8 KB |
| assets/manifest.json | config | 320 B |
| assets/reference.png | binary | 2.7 MB |
| examples/demo.html | other | 30.1 KB |
| examples/foundations.svg | other | 597.0 KB |
| examples/preview.mp4 | binary | 3.2 MB |
| examples/redraw.svg | other | 15.6 MB |
| examples/regions.json | config | 6.7 KB |
| examples/validation.json | config | 2.1 KB |
| profile.json | config | 2.2 KB |
| prompts/author-original.prompt.txt | markdown | 1.2 KB |
| prompts/redraw.prompt.txt | markdown | 1.2 KB |
| references/validation.md | markdown | 1.2 KB |
| references/workflow.md | markdown | 5.0 KB |
| scripts/_contours.py | script | 1.3 KB |
| scripts/_regions.py | script | 1.2 KB |
| scripts/_validate_svg.py | script | 1.5 KB |
| scripts/build_demo.py | script | 5.9 KB |
| scripts/clean_png.py | script | 2.1 KB |
| scripts/export_video.cjs | script | 6.1 KB |
| scripts/package_demo.py | script | 3.4 KB |
| scripts/trace_reference.py | script | 5.0 KB |
| sources.md | markdown | 1.7 KB |
| tests/check_player.cjs | script | 3.9 KB |
| tests/test_pipeline.py | script | 4.2 KB |
| web/load-art.js | script | 3.2 KB |
| web/player.css | other | 738 B |
| web/player.js | script | 2.3 KB |
scripts/trace_reference.py
View on GitHub"""Build native contour paths from the sole supplied reference, without raster embedding."""
from pathlib import Path
from PIL import Image,ImageDraw,ImageFilter
import numpy as np
import heapq,json,time,argparse,hashlib
from _contours import contours
from _regions import read_regions
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('--reference',type=Path,required=True)
parser.add_argument('--regions',type=Path,required=True)
parser.add_argument('--output-dir',type=Path,required=True)
args=parser.parse_args();ROOT=args.output_dir;SOURCE=args.reference
if ROOT.exists():raise ValueError('Choose a new output directory')
ROOT.mkdir(parents=True)
a=np.array(Image.open(SOURCE).convert('RGB'));H,W=a.shape[:2]
# Occupied color bins preserve counts and full-resolution mean colors.
v=a.reshape(-1,3).astype(np.int32)
keys=((v[:,0]>>2)<<12)|((v[:,1]>>2)<<6)|(v[:,2]>>2)
u,inv,counts=np.unique(keys,return_inverse=True,return_counts=True)
sums=np.stack([np.bincount(inv,weights=v[:,c],minlength=len(u)) for c in range(3)],axis=1)
means=sums/counts[:,None]
def stats(ids):
c=counts[ids];m=means[ids];avg=(m*c[:,None]).sum(0)/c.sum();var=((m-avg)**2*c[:,None]).sum(0)
return var,avg
clusters={0:np.arange(len(u))};heap=[];serial=0
var,_=stats(clusters[0]);heapq.heappush(heap,(-float(var.sum()),0));next_id=1
while len(clusters)<4096 and heap:
_,ident=heapq.heappop(heap);ids=clusters.pop(ident)
if len(ids)<2:clusters[ident]=ids;continue
var,_=stats(ids);axis=int(var.argmax());order=ids[np.argsort(means[ids,axis])];weight=counts[order].cumsum();cut=int(np.searchsorted(weight,weight[-1]/2))+1;cut=max(1,min(cut,len(order)-1))
for child in (order[:cut],order[cut:]):
cid=next_id;next_id+=1;clusters[cid]=child
if len(child)>1:
variance,_=stats(child);heapq.heappush(heap,(-float(variance.sum()),cid))
lookup=np.empty(len(u),dtype=np.int32);palette=[]
for index,ids in enumerate(clusters.values()):
_,avg=stats(ids);palette.append(np.rint(avg).astype(np.uint8));lookup[ids]=index
palette=np.array(palette);labels=lookup[inv].reshape(H,W);q=palette[labels]
Image.fromarray(q).save(ROOT/'quantized-review.png')
error=q.astype(float)-a
metrics={'palette_colors':len(palette),'mean_absolute_channel_error':float(abs(error).mean()),'psnr_db':float(10*np.log10(255**2/(error**2).mean())),'exact_pixels_fraction':float(np.all(q==a,axis=2).mean())}
print(json.dumps(metrics),flush=True)
# Regions are meaningful organizational groups, not bitmap tiles. They partition the scene.
regions=read_regions(args.regions,W,H,SOURCE.read_bytes())
mask=Image.new('I',(W,H),0);draw=ImageDraw.Draw(mask)
for i,(_,polygon) in enumerate(regions):
if polygon:draw.polygon(polygon,fill=i)
region=np.array(mask,dtype=np.int32)
combined=labels+region*len(palette)
# Directed boundary edges, with the color region on the right of each edge.
# Each output subpath follows an actual connected color-region contour.
stride=W+1;edge_groups={}
def add(label,start,end):
d=edge_groups.setdefault(int(label),{});s=int(start);e=int(end)
if s not in d:d[s]=e
elif isinstance(d[s],list):d[s].append(e)
else:d[s]=[d[s],e]
for y in range(H):
row=combined[y]
if y==0:
for x in range(W):add(row[x],x,x+1)
else:
xs=np.flatnonzero(row!=combined[y-1])
for x in xs:
s=y*stride+int(x);add(row[x],s,s+1);add(combined[y-1,x],s+1,s)
for x in np.flatnonzero(row[1:]!=row[:-1])+1:
s=y*stride+int(x);add(row[x-1],s,s+stride);add(row[x],s+stride,s)
add(row[0],(y+1)*stride,y*stride)
add(row[-1],y*stride+W,(y+1)*stride+W)
for x in range(W):add(combined[-1,x],H*stride+x+1,H*stride+x)
print('Boundary groups',len(edge_groups),'edges',sum(len(d) for d in edge_groups.values()),flush=True)
by_region={i:[] for i in range(len(regions))};total=0
for num,(code,edges) in enumerate(edge_groups.items()):
d,n=contours(edges,W);rid=code//len(palette);color=palette[code%len(palette)];hexcolor='#'+''.join(f'{v:02x}' for v in color)
by_region[rid].append((hexcolor,d,n));total+=n
if num%2000==0:print('Traced groups',num,flush=True)
# Region silhouettes and a small number of broad tone shapes are a readable foundation.
header=f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {H}" width="{W}" height="{H}" role="img" aria-label="Reference illustration redrawn as native color-region contours">'
svg=[header,'<g id="complete-vector-art" shape-rendering="crispEdges" fill-rule="evenodd">']
for rid,(name,_) in enumerate(regions):
svg.append(f'<g id="{name}" data-region="{name}">')
for i,(color,d,n) in enumerate(by_region[rid]):svg.append(f'<path id="{name}-tone-{i:04d}" fill="{color}" d="{d}"/>')
svg.append('</g>')
svg+=['</g>','</svg>']
text='\n'.join(svg);(ROOT/'faithful.svg').write_text(text)
metrics.update({'svg_bytes':len(text.encode()),'path_elements':sum(len(v) for v in by_region.values()),'connected_contours':total,'method':'native closed color-region contours; no embedded raster or pixel-rectangle grid','regions':[x[0] for x in regions]})
(ROOT/'metrics.json').write_text(json.dumps(metrics,indent=2))
print(json.dumps(metrics),flush=True)
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |