Image2Gcode: Direct G-code from Visual Input
- Image2Gcode is an additive manufacturing framework that directly converts 2D images into executable G-code, bypassing traditional CAD-to-slicer workflows.
- It employs a conditional diffusion-transformer and a 1D U-Net to transform noisy keypoint sequences from slice images into precise print trajectories.
- Empirical evaluations demonstrate 100% execution reliability, diverse infill strategies, and nearly identical travel distances compared to conventional methods.
Image2Gcode is an end-to-end data-driven framework for additive manufacturing that generates printer-ready G-code directly from images and part drawings, bypassing the conventional CAD-to-STL-to-slicer pipeline. In the reported formulation, a hand-drawn or captured 2D image serves as the sole input; the system extracts slice-wise structural cues from that image and applies a denoising diffusion probabilistic model over G-code sequences to produce executable print-move trajectories with associated extrusion parameters. By operating directly on native toolpaths rather than on an explicit 3D model, the framework is positioned as a direct mapping from visual input to material-extrusion (MEX) printer instructions (Wang et al., 25 Nov 2025).
1. Workflow position and problem formulation
Conventional mechanical design and manufacturing workflows begin with conceptual design, followed by construction of a computer-aided design model and fabrication through MEX printing. In that workflow, CAD geometry must be converted into machine-readable G-code through slicing and path planning. Image2Gcode is introduced to remove the CAD stage as a bottleneck, particularly in rapid prototyping contexts where even minor design variations require manual updates in CAD software and make iterative design slow to scale (Wang et al., 25 Nov 2025).
The framework takes two principal forms of visual input. The first is 2D slice images rendered from STL meshes as grayscale images. The second is real-world photographs or hand-drawn sketches that are preprocessed into binary silhouettes. Its output is printer-ready G-code sequences expressed as G1 X… Y… E… commands together with initialization and shutdown commands.
The pipeline consists of three high-level stages: preprocessing, a generative model, and post-processing. Preprocessing extracts per-layer images and aligned toolpath keypoints. The generative component is a diffusion-transformer that maps noisy keypoint sequences conditioned on slice features. Post-processing denormalizes predictions, formats them into G-code, and applies basic validity checks. This organization suggests that Image2Gcode treats toolpath generation as a conditional structured-sequence synthesis problem rather than as explicit geometric reconstruction.
2. Preprocessing, slicing alignment, and tokenization
Training data are derived from the Slice-100K dataset, described as approximately STL–G-code examples. For each STL and G-code pair, the system performs layer-level alignment. It detects -height changes in G-code from G1 commands, slices the STL mesh at the same using PyVista to obtain a grayscale image, and extracts the toolpath as a sequence of points
from cumulative-extrusion G1 commands (Wang et al., 25 Nov 2025).
Normalization is channel-specific. Spatial channels and are centered by the bounding-box midpoint and scaled so that the maximum dimension maps to . The extrusion channel is min–max normalized per layer to 0. Because sequence lengths vary across layers, each sequence is padded or truncated to a fixed length 1, and a binary mask
2
is stored to indicate valid positions.
Each keypoint 3 is treated as a token for the diffusion model. No explicit positional encoding is applied to these tokens; ordering is instead captured through 1D convolutions and self-attention. This design places the sequential structure of print moves inside the denoising backbone rather than in an external geometric graph or symbolic planner.
3. Conditional diffusion-transformer architecture
The vision-conditioning backbone is DinoV2-Small, a Vision Transformer with 4 patches, embedding dimension 5, and 6 layers, producing 7 patch tokens. The encoder is pretrained on 8 M images, and all but the last two layers are frozen during fine-tuning. The resulting multi-scale slice features 9 are supplied to the generative model through cross-attention (Wang et al., 25 Nov 2025).
The denoising process is defined over keypoint sequences. With 0 timesteps, the forward diffusion process is
1
where
2
follows a cosine schedule. The model uses 3-prediction, meaning that it predicts the clean sequence 4 directly from the noisy sequence 5. Reverse transitions are implemented implicitly by iterative denoising.
The denoising backbone is a 1D U-Net. The input sequence 6 is projected to 7 dimensions via a 8 convolution. The network uses five scales with channel multipliers 9. Each stage contains residual blocks with GroupNorm and SiLU, together with strided-convolution downsampling, linear upsampling, and skip connections. Timestep 0 is embedded via sinusoidal encoding and passed through a small MLP before being injected into each block through FiLM-style affine modulation.
Cross-attention is applied at multiple U-Net depths. Keypoint features 1 attend over Dino tokens 2, with 3, according to
4
where the head dimension is 5. The number of attention heads increases in deeper layers for efficiency. A plausible implication is that the architecture uses early visual features for coarse boundary conditioning and deeper sequence representations for more structured toolpath refinement, although the paper states only the placement of cross-attention and not an explicit functional decomposition.
4. Objective, optimization, and implementation choices
The training objective is a masked diffusion loss over valid sequence positions: 6 The mask 7 excludes padded positions. Channel weights are 8 for 9, 0, and 1, respectively, and SNR-based timestep weighting is applied (Wang et al., 25 Nov 2025).
Training uses Slice-100K with a 2 train/validation split on slice-level examples. Only synthetic data are used during training; real photographs and sketches are reserved for zero-shot generalization tests. No additional geometric augmentations are reported.
The implementation is in PyTorch with multi-GPU training. The batch size is 3 per GPU, and training runs for 4 epochs. Optimization uses AdamW with learning rate 5 and weight decay 6. The learning-rate scheduler is ReduceLROnPlateau with factor 7, patience 8, and minimum 9. Gradient clipping is enabled, while mixed precision and checkpointing are disabled.
Several architectural and hyperparameter choices were selected through reported ablation. The U-Net channel multipliers 0 were chosen via early search, 1 was selected as a trade-off between quality and compute, and partial fine-tuning of only the top two DinoV2 layers was found to stabilize training while preserving pre-learned geometric features. The input resolution was fixed at 2, with higher resolutions showing diminishing returns relative to compute cost.
5. Sampling, denormalization, and G-code synthesis
At inference time, sampling begins from
3
followed by iterative denoising for 4. The model produces
5
together with mask 6 (Wang et al., 25 Nov 2025).
Denormalization reverses the preprocessing transformations. For spatial channels, 7 and 8 are restored using cached bounding-box dimensions and midpoint. For extrusion, 9 is restored using the per-layer min–max statistics and then multiplied by a printer-specific extrusion multiplier.
G-code formatting is explicit. The generated file prepends G90, M82, temperature commands, and homing. For each valid keypoint 0, the framework writes
9
and appends standard retraction and motor-off commands. Basic validity checks are then applied: coordinates must remain within build-volume limits, and the 1 channel must increase monotonically. No further geometric repair heuristics are reported.
This post-processing stage is significant because it distinguishes Image2Gcode from systems that stop at geometric prediction. The output is intended to be directly executable by a printer, subject only to the stated formatting and validity constraints.
6. Empirical behavior, generalization, and measured outcomes
The reported qualitative outcome is that generated perimeters accurately follow boundaries and that infill patterns, including hatching, honeycomb, and concentric structures, emerge without rule-based slicing. Physical prints are described as exhibiting good dimensional accuracy and surface quality (Wang et al., 25 Nov 2025).
Generalization is evaluated in a zero-shot setting on photographs and hand sketches after simple edge-based silhouette extraction. The reported behavior is that toolpaths remain coherent and prints succeed across non-polygonal shapes. The paper also reports novel infill diversity: the model can generate multiple valid infill strategies for the same boundary, such as rectilinear versus concentric patterns. This is presented as evidence that the model learns a distribution over feasible toolpaths rather than memorizing a single heuristic mapping.
A quantitative comparison is reported using travel distance, defined as total nozzle motion length per layer. A kernel density estimate yields a mean generated travel distance of 2 units versus heuristic ground-truth at 3 units, corresponding to an approximately 4 reduction. The 5 bootstrap confidence interval is reported as narrow, indicating consistency across runs.
Execution reliability is reported as 6 on held-out slices and real-world inputs. The generated paths are described as comparable to or better than those from a standard CAD-to-slicer pipeline in path efficiency. Because the reported comparisons are centered on execution success and travel distance, a plausible implication is that the principal empirical contribution lies in functional toolpath synthesis rather than in exhaustive benchmarking against full slicer ecosystems.
7. Constraints, failure modes, and research directions
The stated limitations are tightly coupled to the slice-by-slice design. Because the method lacks explicit cross-layer coordination, it cannot handle drastic topology changes between layers. Fine features below 7 mm are occasionally blurred, and corner sharpness is slightly rounded. Dataset diversity is also limited to heuristically sliced patterns, leaving gaps in coverage for exotic infill strategies (Wang et al., 25 Nov 2025).
Scalability limitations are also identified. Using 8 denoising steps incurs inference latency on the order of seconds per layer. Increasing the size of the ViT backbone or raising image resolution would increase memory and compute demand.
Several future directions are proposed. One is a hierarchical 3D pipeline that first generates a small set of key cross-sections and then applies the slice-toolpath model per layer. Another is to introduce explicit conditioning variables such as infill density, pattern type, mechanical performance targets, or natural-language instructions via an LLM embedding. Additional directions include replacing the 1D U-Net with more efficient diffusion architectures such as DDIM or latent diffusion to reduce sampling steps, integrating multi-view or depth cues for full volumetric conditioning, and jointly learning slicing parameters such as layer height and shell count alongside toolpath generation.
These limitations clarify a common misconception that bypassing CAD necessarily yields full geometric autonomy. In the reported system, CAD and STL intermediates are removed from the deployment path, but the learned representation remains layer-conditioned and depends on training data derived from paired STL and G-code examples. The framework therefore redefines where geometric prior knowledge enters the pipeline rather than eliminating such priors altogether.