code-to-image 是做什麼的?
You do not generate pixels. You write code that describes the image, render it with a tool, look at the result, and fix it. Treat every image as compiled code.
1. Pick the format by subject
| Subject | Format |
|---|---|
| Icon, logo, flat illustration, badge | SVG |
| UI screen, card, landing section, poster with text | HTML + CSS |
| Animation, particles, generative / algorithmic art | Canvas or p5.js |
| 3D scene | Three.js |
| Data chart | matplotlib / Plotly / D3 |
| Flowchart, architecture, sequence diagram | Mermaid or Graphviz (DOT) |
| Pixel manipulation of an existing image | Python PIL / OpenCV |
If the user named a format, use it. Otherwise pick the simplest one that fits.
2. Plan before writing code
Write a short plan first (it can be in comments):
- Canvas size and aspect ratio (for example, a 800x600 viewBox).
- A layout grid: main subject position, margins, the reading order.
- Layers from back to front: background, main shapes, details, text.
- A palette of 3–5 colors with enough contrast (aim for WCAG AA against the background for any text).
- Every element gets explicit coordinates. Never say "roughly in the middle". Compute it.
3. Write the code
- Self-contained: inline styles, no external assets unless provided, embed fonts only if needed and fall back to system fonts.
- Use groups (
<g>), named IDs and comments so the image stays editable. - Prefer simple primitives (circle, rect, path with few points) over long freehand paths. Build complex shapes by combining simple ones.
- Keep text as real text, not paths, so it stays crisp and editable. Check that text fits its box.
- For generative art, use a seeded random generator so results are reproducible.
4. Render
Save the code to a file and render it to PNG. Use the bundled helper in this skill's scripts/render.py directory for common formats:
python <skill-dir>/scripts/render.py input.svg # also .html, .mmd, .dot -> input.png
python <skill-dir>/scripts/render.py card.html --width 1080 --height 1080 --no-full-page
Or render directly:
- SVG:
rsvg-convert -w 1200 in.svg -o out.pngor Pythoncairosvg.svg2png(url="in.svg", write_to="out.png") - HTML/Canvas/p5/Three.js: headless browser screenshot (for example, Playwright:
page.goto("file://..."), wait for render,page.screenshot(path="out.png")) - matplotlib:
plt.savefig("out.png", dpi=150, bbox_inches="tight") - Mermaid:
mmdc -i in.mmd -o out.png; Graphviz:dot -Tpng in.dot -o out.png
If no render tool is available, skip to delivery and say the image has not been visually verified.
5. Inspect and fix (max 3 rounds)
Open the PNG and check it with vision:
- Overlapping or clipped elements, text spilling out of boxes
- Elements outside the canvas or crowded against an edge
- Proportions that look wrong, uneven spacing, misaligned items
- Low-contrast text, colors that clash
- Does it actually show what the user asked for?
Fix the specific issue in code and re-render. Stop after 3 rounds or when nothing significant is left.
6. Deliver
- Give the source file (SVG/HTML/py) plus the rendered PNG.
- One line on what it shows and what is easy to change (colors, text, sizes).
Limits and rules
- Code-drawn images are strong for structure (diagrams, icons, UI, charts, typography) and weak for photorealism, faces, fur, complex textures. Say so and offer a stylized version instead of a bad realistic one.
- Do not draw copyrighted characters, mascots, brand logos or reproductions of existing artworks. Create original designs instead.
- Do not depict real identifiable people.