Painting with Code: Using LLMs to Simulate Real Brushstrokes Without Image Models

    This article explores a non-diffusion approach to AI image generation: instead of producing pixels directly, a large language model writes drawing code that simulates real brush movements on a virtual canvas. We break down the architecture, key design choices, and engineering practices behind this technique, including how the model plans strokes, handles irreversibility, and self-corrects through visual feedback. A minimal canvas engine example and practical recommendations are included for developers who want to build their own version.

    Introduction: A Different Path to AI-Generated Art

    In recent years, AI image generation has been almost synonymous with diffusion models: start from random noise, iteratively denoise, and emerge with a complete image. But another approach has been gaining attention in the developer community—skip image generation models entirely, and instead have a large language model write drawing programs that simulate real brush movements on a virtual canvas, painting stroke by stroke.

    This route has sparked widespread interest because the model outputs code, not pixels; mistakes cannot be undone but must be corrected with new strokes; and the system can simulate brush texture, paint wetness, and canvas grain. In a public demonstration, researchers asked a model to create a photorealistic painting. The model responded by writing over a thousand lines of code and executing roughly hundreds of thousands of simulated brushstrokes to produce a realistic painting of a shopping cart in a parking lot—all without any external image model, pre-rendered base image, or tracing reference.

    This article breaks down the principles, architecture, and implementation details of this technique, with illustrative code to help developers understand and reproduce it.

    One: Why Paint with Code? Key Differences from Diffusion Models

    Diffusion models generate images through “global emergence”: they model the statistical distribution of the entire image, denoise step by step, and finally produce a pixel map. Powerful as they are, they have notable drawbacks—the process is opaque and not auditable, and modifying a local region often requires regenerating the whole image. They also struggle to express the order and logic of the creative process.

    Code-driven painting is fundamentally different. Its characteristics include:

    1. Explicit process: Every stroke is a clear instruction (coordinates, brush type, color, pressure, width). The entire creative process is traceable, replayable, and debuggable.
    2. Physical simulation: The code can model brush fiber texture, wet and dry paint mixing, and canvas grain, giving the final image a traditional hand-painted feel.
    3. Local correction: While strokes cannot be undone, they can be covered or adjusted with new strokes, mirroring how human painters work.
    4. Controllable style: By tuning the painting engine’s parameters and the model’s “painting habits,” one can achieve stable, independent artistic styles rather than simply imitating existing painters.

    From an engineering perspective, this is essentially an agent loop of “plan → execute → observe → correct,” where the action space is not API calls but brush strokes.

    Two: Overall Architecture: How the Model “Sees” and “Paints”

    A typical code-painting system consists of three parts:

    • Planning layer (LLM): Responsible for composition, staged planning (base color → form → details → overall adjustment), generating stroke instructions, and self-correcting after observing the canvas.
    • Painting engine (simulator): A virtual canvas implemented in code that renders stroke instructions into pixels and simulates brush physics. It usually includes canvas texture, paint mixing models, and brush shape modules.
    • Observation and feedback loop: After a certain number of strokes, the model “looks at” the current canvas (e.g., by feeding the canvas back as an image to a multimodal model, or by computing programmatic metrics), identifies discrepancies, and adjusts subsequent strokes.

    A minimal painting engine example:

    1. import numpy as np
    2. class Brush:
    3. def __init__(self, width, softness=0.5, bristle_noise=0.1):
    4. self.width = width # brush width in pixels
    5. self.softness = softness # edge softness
    6. self.bristle_noise = bristle_noise # fiber jitter
    7. class Canvas:
    8. def __init__(self, h, w, texture=0.05):
    9. self.image = np.full((h, w, 3), 255, dtype=np.float32)
    10. self.texture = texture # canvas grain intensity
    11. def stroke(self, path_points, color, brush, wetness=0.3, opacity=0.8):
    12. """Draw a stroke along the path, simulating wet paint mixing"""
    13. for x, y in path_points:
    14. self._dab(x, y, color, brush, wetness, opacity)
    15. def _dab(self, x, y, color, brush, wetness, opacity):
    16. # Mix paint around (x,y) using the brush shape:
    17. # Existing color and new paint are weighted by wetness/opacity,
    18. # and we add canvas texture perturbation
    19. ...

    In a real demonstration, the model’s program for a photorealistic work was typically over a thousand lines: first define the palette and brush set, then generate strokes in batches by region (background, subject, shadows, highlights), and finally add details. Each stroke instruction includes path points, color codes (e.g., #30346a), brush width (e.g., 90 pixels), and pressure and wetness parameters.

    Three: Key Design Details

    1. Irreversible Strokes: Turning a Constraint into a Style Source

    During experiments, engineers set strict rules: each stroke is output as a coordinate instruction, and once applied, it cannot be undone. This seems harsh, but it is essential—it forces the model to plan carefully like a human painter, tackling big shapes first and details later. “Mistakes” must be naturally covered by new strokes, resulting in the layered, textured look common in real paintings.

    2. Deliberate Imperfection

    One interesting detail: when drawing long lines, the model actively adds a few pixels of random jitter to the path points, explaining that “human arms cannot draw perfectly straight lines.” This is the key to photorealism—absolutely geometric lines look mechanical, while subtle hand tremors make the image appear human-made.

    3. Observation and Self-Correction

    The model does not write all the code in one go. A typical flow is: pause after several stages, observe the canvas, discover issues such as “back lines too thin” or “contours messy,” then correct with new strokes. This multi-round “paint → observe → adjust” loop is essentially a self-evaluating agent workflow, requiring strong visual understanding and spatial planning from the model.

    4. Style and Persona Training

    In early attempts, the model’s work was criticized for over-imitating existing artistic styles. The researchers addressed this with a novel approach: let the model design its own “artist persona” with fictional experiences and a stable style, then guide it to adjust brush parameters and painting engine behavior through natural language feedback based on the output. This effectively uses prompt engineering and iterative feedback to shape a “style prior,” giving the work independence rather than a collage-like feel.

    Four: Engineering Recommendations

    If you want to reproduce this capability in your own environment, consider the following practices:

    1. Start with simple scenes: Have the model draw a bird at sunrise, a tree, or other clearly outlined subjects. Limit total strokes to the hundreds, and verify that the loop works.
    2. Engine first: Build a solid painting engine first—brush shape, color mixing, and texture overlay are the source of visual quality. The model only makes decisions; the engine handles the physics.
    3. Use staged prompts: Explicitly require “base color blocks first, then form, then details” to prevent the model from getting stuck on local details too early.
    4. Introduce an evaluation loop: After each stage, feed the canvas back to a multimodal model for self-checking, or compute consistency metrics against the target description, to drive corrections.
    5. Compute and storage: A realistic painting may involve hundreds of thousands of stroke calls. Save intermediate rendering snapshots with object storage, run batch experiments on container platforms, and track generation quality with logging and monitoring services.

    Five: Significance and Outlook

    Code painting is not meant to replace diffusion models—those still have a huge advantage in speed and diversity. Rather, it demonstrates the ability of large language models to decompose high-level intent into low-level action sequences. This paradigm of “expressing creativity through programs” is in line with code generation, robot control, and automated scientific experimentation: models become agents that can plan, execute, and correct within constrained environments, not just answer questions.

    As models improve in long-horizon planning and visual feedback, we are likely to see more generative applications that are process-visible, style-independent, and interpretable. For developers, now is a good time to start building a “virtual painting studio.”