Large Language Models Draw Realistic Images with Python Code, No Image Engine Needed

    This article explores an alternative approach to AI image generation, where large language models (LLMs) write Python code to simulate brush strokes on a virtual canvas, producing realistic images without invoking any dedicated image generation engine. It covers the technical principles behind code-driven drawing, the physical simulation of brushes, paint, and canvas, the engineering challenges of large-scale stroke execution, the self-correction mechanism under irreversible strokes, style control via virtual artist personas, and key takeaways for developers. This method reframes image generation as an observable, modifiable process, offering new possibilities for evaluating model reasoning and artistic research.

    1. Introduction: When “Image Generation” Does Not Call an Image Model

    In the rapidly evolving landscape of large models, mainstream AI image generation follows a similar path: text encoding enters a diffusion model, which starts from random noise and progressively removes noise to produce the final image all at once. In this pipeline, the act of “painting” does not truly exist; the model directly outputs pixel results, not the drawing process.

    But another line of exploration takes a completely different approach: let a large language model (LLM) with code generation at its core write Python programs that simulate the movement of a real brush, the wetness of pigment, the texture of brushstrokes, and the feel of a canvas—all on a virtual canvas—to “paint” a work stroke by stroke, without calling any image generation engine. The significance of this capability is not efficiency, but that it restores the image generation process to a “creative process,” offering a new experimental field for model capability evaluation, embodied reasoning, and artistic style research.

    2. Technical Principles of Code-Driven Drawing

    2.1 From One-Shot Generation to Stroke Planning

    Traditional diffusion models produce a complete output; the model does not need to understand “how to paint.” Code-driven drawing requires the model to accomplish two tasks:

    First, planning. The model must mentally break down the target image into composition layers, color zones, light-dark relationships, and a specific sequence of strokes—essentially converting high-level semantics into low-level action instructions.

    Second, execution. Each stroke is expressed as a structured instruction—for example, brush type, color code, brush width, coordinate path, pressure, and wetness parameters. The drawing engine receives these instructions and renders the corresponding effect on the virtual canvas.

    A typical stroke instruction can be abstracted as follows:

    1. stroke = {
    2. "brush": "flat_wash",
    3. "color": "#30346a",
    4. "width_px": 90,
    5. "path": [(x1, y1), (x2, y2), ...], # path points with slight jitter
    6. "paint_wetness": 0.8,
    7. "pressure": 0.6,
    8. }
    9. canvas.apply(stroke)

    2.2 Simulating Realistic Physical Properties

    Such drawing engines typically simulate three layers of physical effects:

    • Brush layer: Different brush shapes (flat, round, fan) determine the edge shape and texture of strokes.
    • Paint layer: The wetness of paint affects diffusion and overlay; wet paints may blend into each other.
    • Canvas layer: Canvas texture causes uneven pigment adhesion, producing the graininess of real paper.

    One notable detail: In some experiments, the model deliberately adds a few pixels of jitter in the first stroke, citing that “human arms cannot draw perfectly straight lines.” This proactive introduction of imperfection shows that the model is not only executing the drawing but also mimicking the hand characteristics of a real creator—a key source of realistic feel.

    3. Engineering Scale and Error Correction Mechanisms

    3.1 Challenges of Large-Scale Stroke Generation

    To achieve near-photorealistic results, a piece may require hundreds of thousands of simulated strokes, often translating into Python programs that are over a thousand or even two thousand lines long. This imposes high demands on the model:

    • Long-range consistency: Tens of thousands of strokes must maintain uniform composition, lighting, and color system; any local deviation can break the whole.
    • Layered organization: Code is typically organized into background, midground, and detail layers, similar to a painter’s process.
    • Computational controllability: Rendering a massive number of strokes requires sensible performance design to avoid uncontrolled simulation costs.

    3.2 Self-Correction under “Irreversible Strokes”

    A more challenging setup is that strokes cannot be undone once applied to the canvas. This means the model must, like a real painter, make cautious judgments before each stroke and, after a stroke is laid down, use new strokes to cover or correct errors. During multiple drawing passes, the model observes the current canvas state, detects issues such as an overly thin outline or messy lines, and then actively plans subsequent strokes to repair them. This “observe-evaluate-correct” loop is essentially a self-iterative reasoning process mediated by visual feedback, whose value extends beyond drawing itself.

    4. Style Control: Constraining Strokes with Virtual Artist Personas

    Another unique advantage of code-driven drawing is that style is explicitly controllable. Because every stroke is a code instruction, adjusting brush parameters, jitter amplitude, color strategy, and so on can systematically change the stylistic tendency of the work.

    A practical method described in the article is to let the model design a fictional artist identity—including background, habitual brush use, and creative philosophy—and then constrain the generation process with that identity’s style. After viewing the output, the model is given text feedback to adjust brush parameters and drawing engine configurations. Compared with simply asking to “imitate a certain artist’s style,” this approach allows the model to develop a relatively independent, internally consistent artistic style and reduces excessive imitation of existing artworks.

    5. What Developers Can Learn

    This “code-driven simulation of real processes” paradigm offers three broad lessons for engineering practice:

    1. Process is capability: Having models output procedural instruction sequences rather than final results can more finely assess their planning, reasoning, and self-correction abilities.
    2. Physical constraints enhance realism: Introducing real-world physical constraints (jitter, humidity, texture) into the generation process often yields more natural outputs than pure mathematical optimization.
    3. Feedback loops are generalizable: Observing the intermediate state, detecting deviations, and correcting with subsequent actions can be transferred to code debugging, automated operations, and other scenarios.

    Code-driven drawing may not replace diffusion-based image generation, but it proves something important: when a large language model is powerful enough, “generating an image” can be reduced to “generating a program that draws the image.” And the program itself becomes a reviewable, modifiable, reusable engineering asset. This is the most relevant aspect of this unconventional direction for developers.