Multimodal LLM Graphical Output: A Practical Guide to Generating Vector Drawing Code from Images
This guide explains how to use multimodal large language models to convert 2D diagrams into compilable, reusable vector drawing code. It breaks the workflow into four stages—image parsing, structural reasoning, code generation, and validation with automated repair—and covers 3D structure and occlusion inference, coordinate mapping, path generation, line styles, fills, and drawing order. It also outlines prompt design, automated checks, and common pitfalls to avoid.
1. Why Generating Drawing Code from Images Is Hard
In real development work, you often need to turn a 2D diagram into vector drawing code that compiles and can be reused. This may look like a simple format conversion, but it requires the model to complete a full reasoning chain: identify geometric elements in the 2D image, infer the corresponding 3D structure, occlusion relationships, and layer membership, and finally map the result to coordinates, paths, solid and dashed lines, fill regions, and drawing order.
The difficulty comes from four main areas:
- Spatial reasoning: 2D projection loses depth information, so the model must reconstruct the structure from line intersections, perspective rules, and shading cues.
- Relationship determination: Which object occludes which, which line is in front, and which faces belong to the same layer all require explicit judgment.
- Symbol constraints: Vector drawing languages have strict syntax requirements. Coordinates, path closure, line types, and fill parameters must be precise.
- Order sensitivity: Drawing order directly determines occlusion effects. A wrong order makes the figure “look wrong.”
2. Overall Technical Approach
Split the task into four stages to form a pipeline that is observable and can roll back:
- Stage 1: Image parsing. Extract primitives, connecting lines, regions, and text labels.
- Stage 2: Structural reasoning. Build a 3D hypothesis and determine occlusion and layers.
- Stage 3: Code generation. Produce a structured intermediate representation, then convert it to the target drawing code.
- Stage 4: Validation and repair. Compile-check, render and review, compare differences, and repair automatically.
The value of this layered design is that each stage has clear inputs and outputs, making it easy to locate problems and to intervene manually at the intermediate representation layer.
3. Engineering Methods for Key Steps
3.1 3D Structure and Occlusion Reasoning
Do not let the model directly “guess” the final code. Have it first output an explicit structural description, such as faces, edges, vertices, and their front-to-back relationships. One possible intermediate representation is:
{"objects": [{"id": "A", "type": "box", "depth": 0},{"id": "B", "type": "plane", "depth": 1, "occludes": ["A"]}],"layers": {"front": ["B"], "back": ["A"]}}
With a structured intermediate layer, code generation only needs a deterministic mapping, which significantly reduces the chance of hallucination.
3.2 Coordinate Mapping and Path Generation
Image coordinates and drawing coordinates often point in opposite directions and use different scales. Define the mapping rules first, then generate paths. In the prompt, explicitly specify the origin, scaling, and flip strategy, and require all coordinates to use consistent precision. Prefer closed paths when generating paths to avoid gaps in filled regions.
3.3 Line Types, Fills, and Drawing Order
Manage solid and dashed lines, fill regions, and drawing order as separate fields rather than mixing them into natural-language descriptions. Organize drawing order as “background layer → structure layer → annotation layer” so that occlusion matches visual expectations.
4. Prompt Design and Constraint Validation
A high-quality prompt should contain four elements: task goal, input description, output format, and prohibited actions. Pair it with automated validation:
- Syntax validation: Confirm the drawing code compiles.
- Structure validation: Check coordinate ranges, path closure, and layer completeness.
- Visual validation: Render the result and compare its similarity to the original image.
- Repair loop: Feed validation errors back to the model and restrict it to fixing only the errors without changing the structure.
5. Common Pitfalls and How to Avoid Them
- Skipping the intermediate representation, which leads to messy code structure.
- Ignoring coordinate flipping, which turns the figure upside down.
- Choosing drawing order arbitrarily, which distorts occlusion relationships.
- Lacking automatic review, so errors go undetected.
6. Conclusion
Converting a 2D diagram into vector drawing code is essentially a combination of spatial understanding, constrained generation, and closed-loop validation. With a structured intermediate representation, clear coordinate rules, layered drawing order, and an automatic repair mechanism, developers can significantly improve the stability and maintainability of multimodal models on graphical output tasks. If combined with the general container platform, logging service, and monitoring and alerting capabilities offered by Baidu AI Cloud, you can further enable task orchestration, quality tracking, and continuous iteration.
