Turning a PNG into a cut file for Cricut and lasers: 4 details that matter
Crafters ask a deceptively simple question: "How do I turn this PNG into a file my Cricut (or laser) can cut?" Most online converters give you an SVG that looks right in a browser but cuts badly: the outline gets cut twi
Crafters ask a deceptively simple question: "How do I turn this PNG into a file my Cricut (or laser) can cut?" Most online converters give you an SVG that looks right in a browser but cuts badly: the outline gets cut twice, letters lose their holes, or the file opens at the wrong size.
I recently built a browser-only tracer for cutting machines. This post covers what a cutting machine actually needs from a vector file, and the four details that broke my first attempts.
A cut file is not just "an SVG"
A blade or laser follows closed outlines. For a file to cut cleanly, it needs:
- Closed, filled shapes, with no strokes. A stroke is painted on both sides of a path, and software disagrees about what to do with it: cut along it, cut both edges, or ignore it. A filled closed path means one thing only.
-
Holes that are holes. The inside of an
Omust be cut out, not filled. -
A real physical size.
width="5in", notwidth="800". Otherwise Design Space and friends guess (often 72 or 96 px per inch). - One group per colour, so a two-colour decal becomes two cut layers.
Here's the shape of a file that meets all four:
<svg xmlns="http://www.w3.org/2000/svg" width="5in" height="3.2in" viewBox="0 0 1000 640">
<g id="colour-1-1d2433" fill="#1d2433">
<path fill="#1d2433" d="M120 80L380 80L380 300L120 300Z M200 150L200 230L300 230L300 150Z"/>
</g>
</svg>
The viewBox stays in traced pixels and width/height carry the physical size, so the scale is exact.
1. Holes: reverse the winding
The path above has two subpaths: the outer square and an inner square. SVG's default fill-rule is nonzero, so the inner square only becomes a hole if it runs in the opposite direction to the outer one. Tracers usually return holes as separate outlines in the same direction. Reverse them before appending them to the parent's d, and the file renders correctly under both nonzero and evenodd:
function reverseSegments(segs) {
// segs: [{type:'L', x1,y1,x2,y2} | {type:'Q', x1,y1,x2,y2,x3,y3}]
const last = segs[segs.length - 1];
const start = last.type === 'Q' ? [last.x3, last.y3] : [last.x2, last.y2];
let d = `M${start[0]} ${start[1]}`;
for (let i = segs.length - 1; i >= 0; i--) {
const s = segs[i];
d += s.type === 'Q' ? `Q${s.x2} ${s.y2} ${s.x1} ${s.y1}` : `L${s.x1} ${s.y1}`;
}
return d + 'Z';
}
To test it, compute the signed (shoelace) area of the outer and hole subpaths. The signs must differ.
2. Background: flood fill from the border, not "delete white"
"Remove all white pixels" also deletes the white inside the letters, which are the holes you want to keep. A better rule: the background is the most common colour on the image border, plus anything connected to the border within a tolerance. A 4-neighbour flood fill from every border pixel does this in O(n), and enclosed white areas survive.
3. Colours: seed k-means from flat colours
Plain k-means++ on a logo spends its clusters on anti-aliasing blends along edges and misses small but important flat colours (a pink flower centre). Seeding from the most populated colour buckets (5 bits per channel) that differ from each other by a minimum distance fixed that. A few Lloyd iterations then refine the centres. It's deterministic, so the same image always gives the same layers.
4. DXF: write R12, flatten curves
For laser and CAD users, DXF R12 (AC1009) is still the most widely readable dialect: closed POLYLINE entities (70 flag 1), a LAYER table with one layer per colour, and Y flipped (DXF's Y axis points up). R12 has no curve type that every program reads the same way, so quadratic segments get flattened to short lines (I use about 0.25 mm). Validate the output with a strict reader such as ezdxf's recover module.
Testing it like a crafter would
Unit tests on synthetic shapes are necessary but not enough. The end-to-end test that caught the most bugs:
- Trace a real sample (logo, line-art JPEG, colour clipart) in a headless browser.
- Download the SVG and assert:
viewBoxpresent, physical units, no<image>/stroke, every subpath ends inZ. - Render the SVG back onto a canvas at the original size and compute the IoU of the "ink" pixels against the original. My samples land between 0.96 and 0.998, so a regression in hole handling shows up immediately as a drop.
Try it
The tracer runs entirely in the browser (a module Web Worker, using the public-domain imagetracerjs for the edge tracing, plus the quantizing and exporters described above): Tracenest. Tracing, preview and a one-colour SVG are free. Multi-colour layers, DXF, PDF and EPS cost $1.99 per image. Images are never uploaded.
If you work on cutting-machine or CAD file formats and know a reader that chokes on R12 polylines, I'd like to hear about it in the comments.
Disclosure: this article was drafted with AI assistance (Claude) and checked against the working, tested code it describes.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.