Pixel2Lines

Service
Photo to SVG Drawing
Photo to SVG Drawing
Photo to SVG Laser Engraving
Photo to SVG Laser Engraving
Photo to SVG Shapes
Photo to SVG Shapes
Photo to Illustration
Photo to Illustration
Floor Plan Conversion
Floor Plan Conversion
Embroidery Digitization
Embroidery Digitization
Photo to Remove Background
Photo to Remove Background
Image Upscaler
Image Upscaler
Design Grid
Design Grid
Contact & Support→Technology→WorkspaceSVG EditorConverters
GalleryPricingSVG EditorWorkspace
  1. Home/
  2. Guides & Resources/
  3. G-Code Guide for CNC, Laser Engravers, and Plotters

The Complete G-Code Guide: From Beginner to Professional

What G-Code is, how machines use it, and the full production pipeline — from SVG and raster images to machine-ready files for pen plotters, laser engravers, 3D printers, and CNC mills.

What Is G-Code?

G-Code (Geometric Code) is the plain-text language that drives CNC machines. Each file — typically .gcode, .nc, or .cnc — is a sequence of instructions telling the machine where to move, how fast, and when to activate its tool. Lines execute top to bottom, one at a time.

Think of your design as the blueprint and G-Code as the turn-by-turn navigation. A 3D printer cannot process a JPG. A pen plotter does not know what the letter 'A' looks like. G-Code solves this by breaking any shape into elementary moves — straight lines, arcs, and tool on/off commands — that any motion controller can execute reliably.

The standard traces back to MIT in the 1950s, formalized as RS-274 in 1963 and internationally published as ISO 6983 in 1982. Despite its age, G-Code remains the universal language of fabrication — from hobbyist desktop printers to industrial five-axis mills.

A G-Code text file open in a text editor showing sequential machine instructions with coordinates and commands
A G-Code file is a plain text document — every line is one precise instruction the machine executes in order.
A pen plotter drawing a detailed vector artwork on paper, showing the physical result of G-Code instructions
The physical result: the same lines of text translated into precise mechanical motion.

Where Is G-Code Used?

  • Pen Plotters (AxiDraw, HP 7475A, DIY GRBL): moves a physical pen across paper to reproduce vector artwork — one of the most accessible entry points into G-Code for artists and makers.
  • Laser Engravers and Cutters: steers the beam while modulating power to burn images into wood or cut shapes from acrylic.
  • FDM 3D Printers (Prusa, Creality, Bambu Lab): coordinates the print head across X, Y, Z while feeding filament, building objects layer by layer.
  • CNC Routers and Mills: directs a spinning cutting tool through material for carving, pocketing, and profiling.
  • CNC Lathes, Plasma Cutters, Waterjet and Wire EDM machines: all use G-Code or a close derivative.

The Anatomy of a G-Code File

Each line (called a block) is one complete instruction. The machine remembers its state between lines — a feedrate set on line 10 stays active on line 200 unless you change it. This is called modal state.

Here is a pen plotter program that draws a 50×50mm square:

G21 ; millimeters

G90 ; absolute positioning

G0 Z5.0 ; lift pen

G0 X0 Y0 ; move to origin

M3 S1000 ; pen down

G1 X50.0 Y0 F2000

G1 X50.0 Y50.0

G1 X0 Y50.0

G1 X0 Y0

M5 ; pen up

M2 ; end

Breaking down G1 X50.0 Y25.3 F1500: G1 = draw a straight line, X50.0 Y25.3 = destination, F1500 = 1500 mm/min. Notice G1 only appears once — every following coordinate line reuses it automatically until you write G0 or another motion command. Anything after a semicolon is a comment, ignored by the machine.

Infographic annotating a single G-Code line showing the G-word command, X Y Z coordinates, feedrate F parameter, and S power parameter with labels and arrows
Anatomy of a G-Code line: G-words control motion type, coordinates define destination, F sets speed, S sets power.

Essential G-Code Commands

These commands work across virtually all firmware — from hobbyist GRBL to industrial Fanuc.

  • G0 — Rapid Move: reposition as fast as the machine allows. Never use with laser or spindle active.
  • G1 — Linear Move: draw or cut a straight line at the set feedrate (F). The primary command for all CNC work.
  • G2 / G3 — Clockwise / Counter-Clockwise Arc: produce smooth curves in a single command using I/J center offsets or R radius. One G2/G3 replaces dozens of tiny G1 segments.
  • G4 — Dwell: pause for a set time. ⚠ P unit varies: GRBL = seconds (G4 P1.5 = 1.5s), Marlin = milliseconds (G4 P1500 = 1.5s).
  • G20 / G21 — Inch / Millimeter units. Always set this at the start of every file.
  • G28 — Home all axes. Behavior varies by firmware — always verify before using.
  • G90 / G91 — Absolute / Relative positioning. G90 is the default; G91 makes every coordinate relative to the current position.
  • G92 — Set current position as origin without moving. Used to define a work origin mid-job.
  • M3 S[value] — Tool On: fires the laser, spins the spindle, or lowers the pen servo. S controls power, RPM, or servo angle.
  • M5 — Tool Off. Always include before any rapid travel move.
  • M104 / M109 — Set hotend temperature (3D printing). M109 waits until target is reached before continuing.
  • M140 / M190 — Set bed temperature (3D printing). M190 waits — use before printing starts.
  • F — Feedrate in mm/min. Modal: persists until you change it.
  • S — Power or speed: laser power (0–1000 on GRBL), spindle RPM, or servo angle.
  • E — Extruder filament distance (3D printing only).
  • I, J — Arc center offsets from current position, used with G2 and G3.
G4 Dwell: Seconds vs Milliseconds

GRBL uses seconds — G4 P1.5 pauses for 1.5 seconds. Marlin uses milliseconds — G4 P1500 is the same pause. Using the wrong unit means your machine either barely pauses or freezes for minutes. Always check your firmware docs.

An SVG file open in Inkscape showing vector paths with anchor points visible
Input: an SVG file with clean vector paths. Each path segment becomes a G-Code motion command.
Diagram showing the workflow from design software through CAM to G-Code file to machine execution
The pipeline: design → CAM/conversion → G-Code → firmware → physical motion.

Workflow 1: Pen Plotter

  1. 1

    Design in vector software

    Plotters only understand paths — not pixels, fills, or raw text. Use Inkscape, Illustrator, or Affinity Designer to create SVG stroke paths. Convert all text to outlines. Remove fills, bitmaps, and effects — they are silently ignored by the converter.

  2. 2

    Optimize the SVG

    Merge duplicate paths (the pen would trace the same line twice), simplify dense nodes, and set document dimensions in millimeters — not pixels — to avoid DPI scaling issues later.

  3. 3

    Convert to G-Code

    The converter translates each path into G1 moves and inserts M3/M5 pen-lift commands between disconnected strokes. The most important setting is path sorting — which order strokes are drawn. A poor sort sends the pen zigzagging across the page thousands of times. A nearest-neighbor sort can cut travel time by 50% or more on dense artwork.

  4. 4

    Send via a G-Code sender

    Stream the file to GRBL firmware through Universal Gcode Sender (UGS) or bCNC. The sender delivers lines one by one; GRBL translates each into stepper motor pulses.

Workflow 2: Laser Engraver / Cutter

  1. 1

    Choose your mode: vector or raster

    Vector mode traces paths at constant power — use it for cutting outlines and engraved lines. Raster mode sweeps back and forth like a printer, varying power pixel-by-pixel — use it for photos and shaded fills. A single job can use both modes on separate layers.

  2. 2

    Generate G-Code with laser settings

    Tools like LightBurn or LaserGRBL output M3 S[value] to fire the laser and M5 to stop it. In raster mode the S value changes on every G1 line, reproducing grayscale gradients. GRBL must be compiled in laser mode for this — it disables speed ramping and enables instant power response.

  3. 3

    Test on scrap first

    Power and speed vary dramatically by material. Always run a small power/speed grid test on the same material before starting the real job.

Laser and CNC Safety

Laser: wear safety glasses rated for your wavelength (CO2 = 10,600nm; diode engravers ≈ 450nm — these require different eyewear). Ensure ventilation — engraving produces toxic fumes from almost all materials. Never leave a running laser unattended. CNC: clamp the workpiece before running the spindle — an unsecured piece can be thrown at speed. Know your emergency stop before you start.

Workflow 3: 3D Printing

  1. 1

    Model and export as STL or STEP

    Design in Fusion 360, Blender, FreeCAD, or similar. STL is the standard exchange format; STEP carries more precise geometry for engineering parts.

  2. 2

    Slice into layers

    A slicer (PrusaSlicer, Cura, Bambu Studio) cuts the model into 0.1–0.3mm horizontal layers and calculates toolpaths, supports, infill, and bridges. A typical move looks like G1 X42.5 Y88.3 E0.0234 F4500 — moving the head while extruding exactly 0.0234mm of filament. The slicer also inserts M109 and M190 to heat the nozzle and bed before printing starts.

  3. 3

    Send via SD card or network

    Marlin, Klipper, or RepRapFirmware executes the file. Klipper offloads computation to a Raspberry Pi and supports input shaping — it measures frame resonance with an accelerometer and filters it out of motion commands, reducing ringing artifacts and enabling higher print speeds.

Workflow 4: CNC Machining

  1. 1

    Model in CAD

    Use Fusion 360, SolidWorks, or FreeCAD. Machined parts require tolerances of 0.01–0.05mm, so model quality directly determines part quality.

  2. 2

    Define toolpaths in CAM

    CAM software (Fusion 360 CAM, VCarve, Mastercam) lets you specify the tool, cutting strategy (adaptive clearing, contour, pocketing), depth of cut, spindle speed, and feedrate. The goal is efficient tool engagement without breaking the bit or burning the material.

  3. 3

    Post-process for your controller

    Industrial CNC machines each speak their own dialect — Fanuc, Siemens Sinumerik, Heidenhain, Haas. A post-processor inside your CAM software translates generic toolpaths into the exact syntax your machine expects. Using the wrong post-processor does not just produce bad parts — it can crash the machine into the workpiece, destroying tooling and creating a safety hazard.

SVG to G-Code: What Actually Happens

SVG paths use Bézier curves, arcs, and straight lines. G1 only draws straight lines — so converters must bridge the gap two ways:

Faceting breaks curves into many tiny straight segments. Smoother curves require shorter segments, which means larger files and potential motion stuttering when the machine's command buffer can't keep up.

Arc fitting is smarter: it detects when a run of short segments collectively forms a circle and replaces the whole group with a single G2 or G3 command. A circle that takes 360 G1 lines becomes one line of G-Code. Files shrink by up to 90%, motion is perfectly smooth, and the machine holds a continuous speed through the arc. Not all GRBL builds support G2/G3 — check before enabling.

Diagram comparing a curve converted to many G1 segments versus a single G2 arc command
The same circle: 360 G1 segments (left) versus one G2 command (right). Arc fitting produces smaller files and smoother motion.
Wrong SVG DPI = Wrong Size Output

Illustrator exports at 72 DPI. Inkscape before v0.92 used 90 DPI. Modern tools use 96 DPI. If your converter assumes 96 DPI but your file came from Illustrator, every dimension is 33% too large — a 100mm shape plots at 133mm. Fix: match your converter's DPI setting to your source app, or better yet, set your SVG document dimensions in millimeters to make DPI irrelevant entirely.

G-Code Dialects: Why One File Doesn't Fit All Machines

Core motion commands (G0, G1, G2, G3) work everywhere. Everything else — startup sequences, tool changes, comment syntax — varies by firmware family. Running G-Code from the wrong controller on a professional CNC machine does not just produce wrong output — it can cause a rapid crash into the workpiece.

  • GRBL: dominant firmware for hobbyist pen plotters, laser engravers, and small CNC routers. Arduino-based, widely supported by converters and CAM tools.
  • Marlin: dominant for FDM 3D printers. Adds extruder control, temperature M-codes, and bed leveling on top of standard motion commands.
  • Klipper: modern 3D printer firmware running on a Raspberry Pi. Enables input shaping and higher print speeds not achievable on Marlin with the same hardware.
  • Smoothieware: 32-bit ARM firmware for mid-range laser engravers and CNCs — more compute headroom than Arduino-based GRBL.
  • Fanuc: dominant industrial CNC controller globally. Includes canned cycles (G81–G89) and macro programming.
  • Siemens Sinumerik / Heidenhain / Haas: European and US industrial controllers with their own dialects. A Fanuc post-processor will not run correctly on a Sinumerik machine.

Converting Photos to Plottable Paths

Photos contain only pixels — no path data. Before a photo can be plotted or vector-engraved, it must be converted to SVG. Common approaches:

  • Line art tracing: extracts the outlines and structural edges of the subject as SVG paths. Best for logos, portraits, and illustrations with clear contours.
  • Hatching / cross-hatching: maps image brightness to line density — darker areas get more closely packed lines. Results evoke traditional engraving and plot beautifully.
  • Stippling: maps brightness to dot density. Each dot is a brief pen touch or laser dwell — similar to pointillist illustration.
  • Contour mapping: treats luminosity like elevation, drawing concentric lines at brightness thresholds. Produces flowing, organic results from photos.
  • Algorithmic styles (Voronoi, flow fields, wave patterns): mathematical transforms modulated by image brightness for abstract but recognizable machine-drawn art.
Original photograph showing natural lighting, tonal variation, and photographic detail
Input: a raster photograph. Pixels only — not directly plottable.
SVG line drawing derived from the photograph, showing clean vector strokes
Output: clean SVG paths, ready for G-Code conversion and physical plotting.

The Complete Pipeline: Photo → SVG → G-Code with Pixel2Lines

Pixel2Lines converts your photo into a clean, machine-ready SVG in professional styles built for pen plotters and laser engravers — line drawing, hatching, stippling, and more. Output paths are structured as discrete strokes, minimizing pen lifts and travel time.

Once you have the SVG, the SVG to G-Code converter generates the final file with full control over feedrate, pen-up height, laser power, and path sort order.

This two-step pipeline — photo to SVG via Pixel2Lines, SVG to G-Code via the converter — takes you from any photograph to a machine-ready file without needing vector design skills or G-Code knowledge.

End-to-end pipeline diagram: raster photo → Pixel2Lines SVG conversion → SVG to G-Code → machine execution
The complete pipeline: photo → SVG (Pixel2Lines) → G-Code (converter) → machine.

Pre-Flight Checklist

  • Simulate first — use NCViewer (browser, free) or CAMotics (desktop, free) to render the full toolpath before the machine moves. Catches wrong sizes, missing pen lifts, and unexpected rapids.
  • Verify units — G20 (inches) or G21 (millimeters) must match your expected dimensions.
  • Set the work origin — machine homed, G92 or WCS offset correctly placed.
  • Check Z clearance — pen-up or laser-off height must physically clear the workpiece and any clamps.
  • Look for duplicate paths — each contour once only; duplicates double-burn or double-cut.
  • Review feedrates — too fast causes skipped steps; too slow wastes time.
  • Confirm G4 dwell units — seconds for GRBL, milliseconds for Marlin.
  • Dry run at safe height — verify the full travel envelope fits the machine's working area.
  • Test on scrap — for laser and CNC always cut the same material on scrap before committing the final piece.

Common Problems and Fixes

  • Drawing is mirrored: SVG Y increases downward; G-Code Y increases upward. Enable Y-axis inversion in your converter.
  • Wrong output size: DPI mismatch. Illustrator = 72, old Inkscape = 90, modern tools = 96. Match converter DPI to your source app, or define SVG dimensions in millimeters.
  • Machine stutters through curves: too many tiny segments overflowing the motion buffer. Enable arc fitting, increase linearization tolerance, or lower feedrate.
  • Pen drags and never lifts: M5 command is missing, or Z clearance is too low to physically lift off the paper.
  • Job takes far longer than expected: poor path ordering. Re-sort paths with vpype before regenerating G-Code.
  • Machine moves to wrong location at start: work origin not set. Re-home, jog to the intended origin, and run G92 X0 Y0 before starting.

Can I write G-Code by hand?

Yes — for simple shapes it is a useful exercise. For anything complex, use CAM software or a dedicated converter.

Is G-Code the same on all machines?

Core motion commands are universal. Startup sequences, tool changes, and extended features differ significantly. G-Code for a GRBL plotter may need substantial changes to run on a Fanuc mill — and using the wrong dialect on an industrial machine can cause a crash.

What is GRBL?

GRBL is an open-source CNC firmware that runs on Arduino-class microcontrollers. It is the standard for hobbyist pen plotters, DIY laser engravers, and small CNC routers. It implements the core RS-274 standard with an acceleration-aware motion planner and look-ahead buffering.

What feedrate should I use for pen plotting?

Ballpoint: 5000–8000 mm/min. Felt-tip or brush pen: 2000–4000 mm/min. Fountain pen or glass nib: 1500–3000 mm/min. Always test on scrap paper first.

Can G-Code control laser power continuously along a move?

Yes. In GRBL laser mode, S can change on every G1 line — this is how raster engraving reproduces smooth grayscale gradients in a single sweep.

What is the difference between G-Code and HPGL?

HPGL (Hewlett-Packard Graphics Language) was used by HP pen plotters from the 1970s–90s. It uses two-letter commands (PU = pen up, PD = pen down, PA = plot absolute) and plotter units of 40 per millimeter instead of mm or inches. Most modern plotter tools can read both formats.

How do I simulate G-Code before running it?

NCViewer (ncviewer.com) is the fastest option — paste your file and it renders the toolpath instantly. CAMotics simulates 3D material removal for CNC work. Universal Gcode Sender has a built-in path preview. Always follow on-screen simulation with a physical dry run at safe height.

Related Guides

How Pen Plotters Work

Pen plotter mechanics, motion control, and pro features that shape speed, accuracy, and line quality.

SVG Optimization for Pen Plotting

How to prepare SVG files for the best possible plotter output — node reduction, path merging, layer structure, and export settings.

How to Convert a Photo to an SVG Line Drawing

Step-by-step guide to converting photographs into production-ready SVG line drawings for CNC, laser, vinyl, and pen plotting workflows.

Hatching and Cross-Hatching with Pen Plotters

How to generate hatching patterns from grayscale images — density mapping, angle variation, and cross-hatch techniques.

Convert a Photo or SVG to G-Code — Ready for Your Machine

Upload any photo to get a clean, optimized SVG from Pixel2Lines, then convert it to machine-ready G-Code in one step. Works for pen plotters, laser engravers, and CNC machines.

Convert Your Photo Now

Want to clean or measure your SVG first?

Open the free SVG editor in your browser to inspect scale, clean paths, and export a production-ready file without uploading it.

Comments

Please login or create an account to write a comment.

Login or Signup

Loading comments...

Pixel2Lines

Workflow Services:


  • Photo to SVG DrawingVector
  • Photo to SVG Laser EngravingVector
  • Photo to Remove BackgroundRaster
  • Photo to SVG ShapesVector
  • Photo to Embroidery DigitizationVector
  • Photo to IllustrationRaster
  • Photo to Design GridRaster
  • Gallery
  • Pricing
  • About Us
  • Technology
  • Custom Development
  • Contact Support

Conversion Tools:


  • File Converters
  • JPG to PNG
  • JPG to WEBP
  • JPG to AVIF
  • JPG to ICO
  • PNG to JPG
  • PNG to AVIF
  • PNG to WEBP
  • PNG to ICO
  • WEBP to JPG
  • WEBP to PNG
  • WEBP to AVIF
  • AVIF to JPG
  • AVIF to PNG
  • AVIF to WEBP
  • SVG to PNG
  • SVG to JPG
  • SVG to WEBP
  • SVG to AVIF
  • SVG to PDFPremium
  • SVG to EPSPremium
  • SVG to AIPremium
  • SVG to DXFPremium
  • SVG to GCODEPremium
  • PDF to PNG
  • BMP to PNG
  • DXF to SVGPremium

Guides


  • Helpful Guides
  • Legal
  • Privacy Policy
  • Terms
  • Cookies