
A real board an AI built with this skill.Open the real board
What you'll get
- Layout
- Grid
- Font
- Handwritten
Instructions
What your AI reads before it draws a board with this skill.
- name
- sketch-tracing
- description
- Use when the user shares a drawing, sketch, line art or photo of paper and wants it redrawn on the board as clean, smooth pen strokes they can edit, recolor and move.
Sketch tracing
Redraw a picture the user gives you as clean freedraw pen strokes on the board. A script finds the center line of every drawn line, follows it through crossings, joins broken pieces, drops lines that retrace another, smooths each one and keeps only the points that matter; the board then draws each stroke as a smooth Catmull-Rom curve through them. The result is real pen strokes, not a pasted image.
Works on line art: pencil sketches, ink drawings, manga pages, doodles, and colored cartoons with dark outlines, on paper or on a dark background. On a photo with no drawn lines it only finds edges, so tell the user that before you start.
Steps
- Get the image as a file you can open: the path the user gave, or the attachment in your code sandbox. Never retype an image's bytes.
- Install the libraries once:
pip install numpy pillow scipy scikit-image. - Save the tracer below as trace_sketch.py and run it:
python trace_sketch.py picture.png trace --width 1000Add --x and --y to place it beside existing content. It prints the drawing size and writes trace-0.json, trace-1.json, ... with up to 150 strokes each. - Send each file, unchanged, as the items of one add_elements call. Do not set color: strokes use the theme ink, so they read in light and dark. Do not set smooth; the default is the smooth curve.
- Draw only the picture. No frame, title, caption or notes unless the user asks for them.
- Run check_board on the drawing's area (render light) and compare it with the original.
Tuning
- Lines missing: lower --sensitivity (0.6). Too much shading or texture: raise it (1.2).
- Thin, fine linework (pencil, small images): --sigmas 1.5,2.5 --line-width 7 --size 1000.
- Still broken into pieces: raise --gap (24). Wobbly curves: raise --smooth (4).
- Big, detailed pictures: raise --max-strokes (900, which is six calls). Keep a trace under about 900 strokes; past that the board gets heavy and each call is large.
After tracing
Offer, do not do it unasked: recolor parts with update_elements, add flat color shapes behind the lines and send them to the back, or remove stray strokes the user points at. To redo a trace, remove the old strokes by their ids (read_board on the area lists them), never by region.
Tracer
import argparse
import json
import numpy as np
from PIL import Image
from scipy import ndimage
from skimage.filters import apply_hysteresis_threshold, sato, threshold_otsu
from skimage.morphology import remove_small_objects, skeletonize
NEIGHBOURS = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
def load_ink(path, size, line_width):
"""Thin dark lines against whatever surrounds them: paper, colour fills or a dark background.
Two passes: lines darker than both sides, then pixels close to the line colour learned from
the first pass, which catches outlines that run along a background as dark as the line.
"""
image = Image.open(path).convert("RGB")
scale = size / max(image.size)
image = image.resize((round(image.width * scale), round(image.height * scale)), Image.LANCZOS)
rgb = np.asarray(image, dtype=np.float32) / 255.0
gray = rgb @ np.array([0.299, 0.587, 0.114], dtype=np.float32)
footprint = int(line_width * 2) | 1
around = ndimage.grey_closing(ndimage.gaussian_filter(gray, 0.7), size=(footprint, footprint))
dark = np.clip((around - gray) / np.maximum(around, 0.08), 0, 1)
dark = np.clip(dark / (np.percentile(dark, 99.7) or 1), 0, 1)
strong = dark > 0.6
if strong.sum() < 50:
return dark
line = np.median(rgb[strong], axis=0)
spread = np.median(np.linalg.norm(rgb[strong] - line, axis=2 if rgb[strong].ndim == 3 else 1))
distance = np.linalg.norm(rgb - line, axis=2)
near = np.clip(1 - distance / max(0.12, spread * 3), 0, 1)
thin = near - ndimage.grey_opening(near, size=(footprint, footprint))
return np.maximum(dark, np.clip(thin, 0, 1))
def trace(skeleton, max_turn=1.05):
on = set(zip(*np.nonzero(skeleton)))
def neighbours(p):
y, x = p
return [(y + dy, x + dx) for dy, dx in NEIGHBOURS if (y + dy, x + dx) in on]
degree = {p: len(neighbours(p)) for p in on}
used = set()
def heading(path):
a, b = path[max(0, len(path) - 6)], path[-1]
return np.arctan2(b[0] - a[0], b[1] - a[1])
def turn(cur, n, h):
d = np.arctan2(n[0] - cur[0], n[1] - cur[1]) - h
return abs((d + np.pi) % (2 * np.pi) - np.pi)
def walk(start, nxt):
path = [start, nxt]
used.add(frozenset((start, nxt)))
while True:
cur = path[-1]
options = [n for n in neighbours(cur) if frozenset((cur, n)) not in used]
if not options:
break
if len(options) > 1 or degree[cur] > 2:
h = heading(path)
best = min(options, key=lambda n: turn(cur, n, h))
if turn(cur, best, h) > max_turn:
break
else:
best = options[0]
used.add(frozenset((cur, best)))
path.append(best)
return path
paths = []
ends = sorted(p for p, d in degree.items() if d == 1)
for start in ends + sorted(p for p, d in degree.items() if d != 1):
for n in neighbours(start):
if frozenset((start, n)) not in used:
paths.append([(float(x), float(y)) for y, x in walk(start, n)])
return paths
def length(path):
a = np.asarray(path)
return float(np.hypot(*np.diff(a, axis=0).T).sum()) if len(a) > 1 else 0.0
def direction(path, at_end, span=8):
a = np.asarray(path)
if at_end:
v = a[-1] - a[max(0, len(a) - span)]
else:
v = a[0] - a[min(len(a) - 1, span)]
n = np.hypot(*v)
return v / n if n else v
def join_all(paths, gap, max_angle):
"""Greedy join, one pass over sorted candidate pairs per round."""
cos_limit = np.cos(max_angle)
paths = [p for p in paths if len(p) > 1]
for _ in range(6):
ends = []
for i, p in enumerate(paths):
ends.append((i, True, np.asarray(p[-1]), direction(p, True)))
ends.append((i, False, np.asarray(p[0]), direction(p, False)))
pts = np.array([e[2] for e in ends])
cands = []
grid = {}
for k, (_, _, p, _) in enumerate(ends):
grid.setdefault((int(p[0] // gap), int(p[1] // gap)), []).append(k)
for a, (ia, enda, pa, da) in enumerate(ends):
gx, gy = int(pa[0] // gap), int(pa[1] // gap)
for ox in (-1, 0, 1):
for oy in (-1, 0, 1):
for b in grid.get((gx + ox, gy + oy), []):
if b <= a:
continue
ib, endb, pb, db = ends[b]
if ib == ia:
continue
dist = float(np.hypot(*(pb - pa)))
if dist > gap or np.dot(da, -db) < cos_limit:
continue
if dist > 1 and np.dot(da, (pb - pa) / dist) < cos_limit:
continue
cands.append((dist + 10 * (1 - float(np.dot(da, -db))), a, b))
if not cands:
break
cands.sort()
taken_end = set()
parent = list(range(len(paths)))
def root(i):
while parent[i] != i:
parent[i] = parent[parent[i]]
i = parent[i]
return i
links = []
for _, a, b in cands:
if a in taken_end or b in taken_end:
continue
ia, ib = ends[a][0], ends[b][0]
if root(ia) == root(ib):
continue
taken_end.update((a, b))
parent[root(ia)] = root(ib)
links.append((a, b))
if not links:
break
nxt = {}
for a, b in links:
nxt[a] = b
nxt[b] = a
done = set()
out = []
for i in range(len(paths)):
if i in done:
continue
start_end = None
for e in (2 * i, 2 * i + 1):
if e not in nxt:
start_end = e
if start_end is None:
start_end = 2 * i + 1
chain = []
idx, e = i, start_end
while True:
done.add(idx)
p = paths[idx]
chain += p[::-1] if ends[e][1] else p
other = e ^ 1
if other not in nxt:
break
e = nxt[other]
idx = ends[e][0]
if idx in done:
break
out.append(chain)
paths = out
return paths
def resample(path, step):
a = np.asarray(path)
seg = np.hypot(*np.diff(a, axis=0).T)
s = np.concatenate([[0], np.cumsum(seg)])
if s[-1] < step:
return a
t = np.linspace(0, s[-1], max(2, int(s[-1] / step) + 1))
return np.stack([np.interp(t, s, a[:, 0]), np.interp(t, s, a[:, 1])], axis=1)
def smooth(a, sigma):
if len(a) < 5:
return a
out = np.stack([ndimage.gaussian_filter1d(a[:, k], sigma, mode="nearest") for k in (0, 1)], axis=1)
out[0], out[-1] = a[0], a[-1]
return out
def rdp(points, epsilon):
if len(points) < 3:
return points
a, b = points[0], points[-1]
ab = b - a
norm = np.hypot(*ab) or 1.0
rel = points[1:-1] - a
distances = np.abs(ab[0] * rel[:, 1] - ab[1] * rel[:, 0]) / norm
i = int(np.argmax(distances))
if distances[i] > epsilon:
left = rdp(points[: i + 2], epsilon)
return np.concatenate([left[:-1], rdp(points[i + 1 :], epsilon)])
return np.stack([a, b])
def dedupe(strokes, shape, radius, min_new, overlap):
"""Drop stretches that retrace a stronger stroke already kept.
Only covered stretches longer than `overlap` are cut, so crossing lines stay whole.
"""
covered = np.zeros(shape, bool)
cross = ndimage.generate_binary_structure(2, 1)
kept = []
for s in strokes:
a = s["px"]
yi = np.clip(a[:, 1].round().astype(int), 0, shape[0] - 1)
xi = np.clip(a[:, 0].round().astype(int), 0, shape[1] - 1)
hit = covered[yi, xi]
step = np.hypot(*np.diff(a, axis=0).T) if len(a) > 1 else np.zeros(0)
cut = np.zeros(len(a), bool)
start = None
for k, h in enumerate(np.append(hit, False)):
if h and start is None:
start = k
elif not h and start is not None:
if step[start : k - 1].sum() >= overlap:
cut[start:k] = True
start = None
lo = None
for k, c in enumerate(np.append(cut, True)):
if not c and lo is None:
lo = k
elif c and lo is not None:
part = a[lo:k]
if length(part) >= min_new:
kept.append({**s, "px": part})
lo = None
mask = np.zeros(shape, bool)
mask[yi, xi] = True
covered |= ndimage.binary_dilation(mask, cross, iterations=radius)
return kept
def main():
parser = argparse.ArgumentParser(description="Trace a drawing into Drawdy freedraw strokes.")
parser.add_argument("image")
parser.add_argument("out", help="output prefix: writes <out>-0.json, <out>-1.json, ...")
parser.add_argument("--size", type=int, default=1400, help="analysis resolution, longest side in px")
parser.add_argument("--width", type=float, default=1000, help="drawing width on the board")
parser.add_argument("--x", type=float, default=0)
parser.add_argument("--y", type=float, default=0)
parser.add_argument("--sigmas", default="2,3.5", help="line thicknesses to look for, in analysis px")
parser.add_argument("--line-width", type=float, default=12, help="thickest line to keep, in analysis px")
parser.add_argument("--sensitivity", type=float, default=0.9, help="higher drops fainter lines")
parser.add_argument("--gap", type=float, default=16, help="join stroke ends this close that keep the same heading")
parser.add_argument("--min-length", type=float, default=18)
parser.add_argument("--smooth", type=float, default=3)
parser.add_argument("--epsilon", type=float, default=1.2)
parser.add_argument("--dedupe", type=int, default=4, help="drop lines retracing a stronger one this close")
parser.add_argument("--max-strokes", type=int, default=450)
parser.add_argument("--chunk", type=int, default=150, help="strokes per add_elements call")
args = parser.parse_args()
ink = np.clip(load_ink(args.image, args.size, args.line_width), 0, 1)
ridges = sato(ink, sigmas=[float(v) for v in args.sigmas.split(",")], black_ridges=False)
ridges = ridges / (ridges.max() or 1)
high = max(0.06, threshold_otsu(ridges) * args.sensitivity)
mask = apply_hysteresis_threshold(ridges, high * 0.5, high)
mask = remove_small_objects(mask, max_size=12)
skeleton = skeletonize(mask)
soft = ndimage.gaussian_filter(ink, 1.5)
paths = [p for p in trace(skeleton) if length(p) >= 4]
paths = join_all(paths, args.gap, np.radians(40))
strokes = []
for p in paths:
a = smooth(resample(p, 1.5), args.smooth)
ln = length(a)
if ln < args.min_length:
continue
yi = np.clip(a[:, 1].round().astype(int), 0, ink.shape[0] - 1)
xi = np.clip(a[:, 0].round().astype(int), 0, ink.shape[1] - 1)
dark = float(np.mean(soft[yi, xi]))
strokes.append({"px": a, "dark": dark, "score": ln * (0.2 + dark) ** 2})
strokes.sort(key=lambda s: -s["score"])
strokes = dedupe(strokes, ink.shape, args.dedupe, args.min_length, 4 * args.dedupe + 6)
for s in strokes:
s["score"] = length(s["px"]) * (0.2 + s["dark"]) ** 2
strokes.sort(key=lambda s: -s["score"])
strokes = strokes[: args.max_strokes]
scale = args.width / ink.shape[1]
items = []
for s in strokes:
pts = rdp(s["px"], args.epsilon)
items.append(
{
"type": "freedraw",
"points": [[round(args.x + x * scale), round(args.y + y * scale)] for x, y in pts],
"strokeWidth": round(1.4 + 1.6 * min(1.0, s["dark"] * 1.8), 1),
"opacity": round(0.7 + 0.3 * min(1.0, s["dark"] * 2), 2),
}
)
files = []
for i in range(0, len(items), args.chunk):
files.append(f"{args.out}-{i // args.chunk}.json")
with open(files[-1], "w") as f:
json.dump(items[i : i + args.chunk], f, separators=(",", ":"))
height = ink.shape[0] * scale
print(f"{len(items)} strokes, {sum(len(i['points']) for i in items)} points, drawing {args.width:.0f} x {height:.0f}")
print("send each file as one add_elements call:", " ".join(files))
if __name__ == "__main__":
main()


