Multimodal Vector Drawing: A Technical Breakdown from Image Understanding to SVG Code Generation

    This article breaks down the technical pipeline for converting a 2D diagram into compilable vector drawing code using multimodal models. It explains why the task is difficult across the semantic, geometric, and expression layers, then walks through a five-stage workflow covering coordinate system conventions, layer separation, occlusion and 3D structure reasoning, geometric primitive normalization, and drawing order. It also provides prompt constraints, quality validation methods, common pitfalls, and a general architecture for production deployment.

    1. Problem Background: Why “Reading a Diagram and Drawing It” Is Harder Than It Looks

    In multimodal applications, one class of tasks looks simple but is technically demanding: given a 2D schematic, the model must output compilable vector drawing code. The source scenario is exactly this—the image contains 3D structures, occlusion relationships, and overlapping layers. The model must both understand the image and reproduce it correctly, translating its understanding into coordinates, paths, solid and dashed lines, fill regions, and drawing order.

    The difficulty breaks down into three layers:

    1. Semantic layer: Identifying which objects are present, their spatial relationships, and their semantic roles (which is the subject, which is the background, which line is an annotation).
    2. Geometric layer: Reconstructing objects from pixel space into reusable geometric primitives (points, line segments, polygons, arcs, Bézier curves) while handling proportion distortion caused by perspective.
    3. Expression layer: Mapping geometric primitives to the syntax of the target drawing language, including coordinate system conventions, path closure rules, line style and fill attributes, and the drawing sequence that determines the final visual result.

    Many failure cases are not caused by the model failing to recognize the image, but by systematic deviations in the geometric and expression layers: coordinate offsets, reversed occlusion order, lost dashed-line styles, and fill regions that overflow. Therefore, engineering practice must break the task into a chain rather than expecting a single generation to get everything right.

    2. Pipeline Breakdown: Five Stages from Pixels to Vector Code

    1. Image Preprocessing and Coordinate System Convention

    Before generation, unify coordinate system semantics: origin position, y-axis direction, and unit scale. Explicitly tell the model whether to use “origin at bottom-left, y-axis pointing up” or “origin at top-left, y-axis pointing down”—the two must not be mixed. For the input image, use proportional scaling with padding to avoid non-proportional stretching that would break geometric proportions.

    2. Structure Recognition and Layer Separation

    Have the model output structured intermediate results first, instead of writing code directly. The intermediate result should include an object list, bounding boxes, and layer labels (background / structure / annotation). The benefit of layering is that generation and validation can proceed layer by layer, and occlusion relationships can be expressed explicitly via “layer order + z-order.”

    3. Occlusion and 3D Structure Reasoning

    The sense of 3D in a 2D image usually comes from perspective, intersecting lines, and occlusion. In practice, require the model to explicitly annotate: which edges are occluded (shown as dashed or broken lines), which faces are visible, and where intersecting lines cross. Describing “invisible parts” separately from “visible parts” significantly reduces path-crossing errors.

    4. Geometric Primitives and Coordinate Normalization

    Convert recognition results into a primitive list, where each primitive specifies type, control points, and style attributes. Normalize coordinates to 0–1 or a fixed canvas size to make secondary scaling easier. A schematic intermediate structure:

    1. {
    2. "canvas": {"width": 800, "height": 600, "origin": "bottom-left"},
    3. "layers": [
    4. {"id": "bg", "z": 0, "fill": "none"},
    5. {"id": "body", "z": 1, "fill": "#e8eefc"},
    6. {"id": "hidden", "z": 2, "stroke": "dashed"},
    7. {"id": "annot", "z": 3, "stroke": "solid"}
    8. ],
    9. "primitives": [
    10. {"type": "polyline", "layer": "body", "points": [[120,80],[520,80],[520,420],[120,420],[120,80]]},
    11. {"type": "line", "layer": "hidden", "style": "dashed", "from": [120,420], "to": [320,520]}
    12. ]
    13. }

    5. Code Generation and Drawing Order

    Follow the principle of “background before subject, fill before stroke, visible before hidden.” Fill regions must close the path before filling; otherwise, color leaks or overflow will occur. Line style attributes (solid / dashed / dash-dot) should be declared uniformly as style fields, avoiding repeated hard-coding in every path.

    3. Prompt and Constraint Design

    Stable output depends on strong constraints. It is recommended to fix four things in the prompt: coordinate system convention, canvas size, style enumeration, and output as compilable code with no added explanation. Example constraint snippet:

    1. - Coordinate system: origin at bottom-left, y-axis pointing up, canvas 800x600.
    2. - Allowed line styles: solid / dashed / dotted only.
    3. - Output the JSON of layers and primitives first, then the final drawing code.
    4. - All fill paths must be closed; occluded edges use dashed.
    5. - Do not output natural language explanations; output code blocks only.

    This two-stage constraint of “intermediate representation first, target code second” is more controllable than one-shot generation and easier to validate programmatically.

    4. Quality Validation and Common Pitfalls

    1. Compilation validation: After generation, run syntax compilation first. If it fails, return the error message so the model can self-repair, with a limited number of retries.
    2. Geometric validation: Check whether bounding boxes are out of bounds, whether paths are closed, and whether coordinates are finite numbers.
    3. Visual regression: After rendering, compare structural similarity against the original image, focusing on whether occlusion and line styles match.
    4. Common pitfalls: Mixing y-axis directions flips the whole image upside down; dashed-line attributes are lost; filling before stroking causes edges to be covered; perspective proportions are broken by non-proportional scaling.

    5. Engineering Implementation Recommendations

    To productize this capability, a general architecture of “queue + object storage + containerized inference + logging and monitoring alerts” can be adopted: requests enter a message queue for peak shaving, images are stored in object storage, the inference service scales elastically via containers, and logging services with monitoring alerts track failure rates and retry counts. Baidu AI Cloud can provide corresponding support in general cloud product categories, but specific selection should be evaluated based on business load and compliance requirements.

    Overall, building “generating vector code from an image” as an observable, verifiable, and rollback-capable pipeline is more realistic than pursuing perfection in a single generation, and it more easily maintains stable output quality in complex schematic scenarios.