How Multimodal LLMs Convert Images into Vector Drawing Code
This article breaks down how multimodal large language models turn a 2D image into compilable vector drawing code. It explains why the task is harder than describing an image, the four reasoning layers involved (2D-to-3D reconstruction, occlusion and visibility, layer and draw-order inference, and structured instruction generation), and a four-stage engineering pipeline covering preprocessing, an intermediate representation, constraint validation, and visual regression comparison. It also shares prompt design practices, common failure modes with mitigations, a three-layer evaluation approach, and notes on running the pipeline on cloud infrastructure.
1. Background: From “Viewing an Image” to “Writing Compilable Drawing Code”
In scenarios such as document automation, teaching material generation, and engineering drawing assistance, a common requirement is to take a 2D schematic as input and produce compilable vector drawing code as output. The example in the source material is representative: the schematic itself is not complex, but it raises the bar for the model considerably. The model must recognize the 3D structure implied by a 2D image, determine occlusion relationships and front-to-back layer ordering between objects, and then map that understanding into structured instructions such as coordinates, paths, solid and dashed lines, fill regions, and drawing order.
The difficulty here is not “describing the image content” but “generating executable, visually consistent structured output.” Natural language descriptions tolerate ambiguity, whereas drawing code is strongly constrained: a coordinate off by one position, an incorrect path-closure condition, or a reversed drawing order will all cause the final figure to deviate noticeably from the input schematic.
2. Capability Breakdown: Four Layers of Reasoning
2.1 Reconstructing 3D Structure from 2D Input
Schematic drawings often use perspective, axonometric, or isometric projection to represent solid objects. The model must infer length, width, and height proportions, vanishing directions, and the connectivity between faces from projection cues, then form an internal approximation of a 3D structure that can be expressed through 2D drawing instructions.
2.2 Occlusion and Visibility Judgment
Occlusion determines which edges should be visible and which should be hidden. In engineering practice, visible outlines are usually drawn as solid lines, while occluded portions are drawn as dashed lines or omitted. If the model judges this incorrectly, the figure takes on a “see-through” look, one of the most common failure modes.
2.3 Layer and Drawing Order Inference
Vector drawing follows the rule that later drawing covers earlier drawing. Fill regions must be drawn before the lines and text on top of them, and distant objects must be drawn before nearer ones. A wrong drawing order breaks occlusion relationships: even with perfectly correct coordinates, the visual result is still wrong.
2.4 Structured Instruction Generation
The final output must include coordinate definitions, path descriptions, line types and line widths, fill styles, transforms, and grouping, while remaining syntactically correct and compilable by a parser.
3. Engineering Practice: A Four-Stage Pipeline
Stage 1: Image Preprocessing and Structural Prompting
Start with size normalization and contrast enhancement, and overlay grid guide lines when necessary to help the model build a sense of coordinates. Then ask the model to first output a “structure inventory,” such as an object list, relative positions, and occlusion pairs, rather than writing code immediately.
Stage 2: Intermediate Representation First
Have the model produce an intermediate representation that is independent of the drawing syntax, then convert it into the target code. Example:
scene:objects:- id: boxtype: cuboidanchor: [0, 0]size: [3, 2, 1.5]view: isometricocclusions:- [box, cylinder, box_in_front]layers:back: [grid]mid: [box]front: [cylinder, labels]
This “semantics first, syntax second” layered design significantly reduces coordinate and ordering errors.
Stage 3: Constraint Validation
Add automated checks before compilation: whether coordinates are out of bounds, whether paths are closed, whether fill regions self-intersect, whether dashed lines are covered by solid ones, and whether layer order satisfies the occlusion pair constraints. When validation fails, feed the error messages back to the model for targeted correction instead of a full rewrite.
Stage 4: Visual Regression Comparison
Render the compiled result into a bitmap and compare its structural similarity against the input schematic. After locating the deviating regions, regenerate only the relevant fragments. Compared with a one-shot full regeneration, local correction converges faster and costs less.
4. Prompt and Context Design Essentials
- Define an explicit output contract: fixed field names, fixed units, and a fixed line-type enumeration to reduce ambiguity.
- Apply step-wise constraints: structure first, then coordinates, then styling, so the model does not juggle too many objectives at once.
- Provide a minimal example: one very simple sample showing how dashed lines and fills are written works better than lengthy textual description.
- Restrict invented syntax: require the model to use only the existing instruction set of the target drawing language.
- Preserve failure information: pass the raw compilation errors back to form a closed loop.
5. Common Failure Modes and Mitigations
| Failure Mode | Symptom | Mitigation |
|---|---|---|
| Occlusion misjudgment | See-through lines appear | Explicitly output occlusion pairs and validate ordering |
| Proportion distortion | Solid objects look flattened | Add axonometric references during preprocessing |
| Ordering errors | Fills cover outlines | Enforce layer separation and redraw on top |
| Syntax out of bounds | Fails to compile | Restrict the instruction set and run a syntax pre-check |
| Local overfitting | Detail piling up, overall offset | Build the overall skeleton first, then refine locally |
6. Evaluation Recommendations
Evaluation should not look only at “whether it compiles.” A three-layer set of metrics is recommended: structural (whether object counts and relative positions are correct), relational (whether occlusion and layer ordering satisfy constraints), and visual (structural similarity between the rendered image and the input image). Only the three layers combined reflect real usability.
7. Integration with Cloud Engineering Capabilities
At scale, image preprocessing and rendering can run on a container platform, task scheduling can be handled by a message queue, intermediate representations and versioned artifacts can be stored in object storage, and compilation and validation failure logs can go into a logging service, with monitoring and alerting tracking success rate and average correction rounds. The general cloud product categories offered by Baidu AI Cloud can support the elasticity and observability requirements of such a pipeline.
8. Conclusion
The core of image-to-vector-code conversion is not “generating text” but “establishing verifiable intermediate semantics.” Making 3D structure, occlusion relationships, and layer ordering explicit, then combining constraint validation with local iterative correction, turns a one-off model output into a stable, reusable engineering pipeline.
