How Large Language Models Simulate Paintbrushes with Code: A Technical Guide to Procedural Painting
Procedural painting uses large language models to write code that simulates brush movements on a virtual canvas instead of generating images directly. This article explains its system architecture, brush and paint physics, irreversible strokes, visual feedback loops, style development, differences from diffusion models, and practical implementation steps.
How Large Language Models Simulate Paintbrushes with Code: A Technical Guide to Procedural Painting
1. Introduction: An Alternative Approach to AI Image Generation
When people think of AI-generated images, diffusion models are usually the first technology that comes to mind. Starting from random noise, they gradually produce an image through multiple denoising steps guided by a prompt. This is a form of global generation: pixels converge statistically over time, without a traditional sense of painting one stroke at a time.
A different approach has recently emerged: instead of generating an image directly, a large language model writes program code that simulates the movement of a real paintbrush on a virtual canvas. The model completes the image stroke by stroke.
In one reported experiment, a large model created a parking-lot scene using approximately 700,000 simulated brush strokes. In another, a model produced a bird-themed artwork with only a short process and slightly more than one hundred strokes. These works did not depend on external image-generation models, pre-rendered images, or tracing templates. Each image was essentially a painting program containing roughly 1,000 to 2,000 lines of code.
The significance of this procedural painting approach is that it transforms painting from “generating an image” into “executing a creative process.” This introduces a new engineering paradigm for AI-assisted art.
2. Core Architecture of a Procedural Painting System
A procedural painting system typically consists of three components:
- Planning layer, or large language model: Understands the creative objective, designs the composition, breaks the task into painting steps, and outputs a sequence of brushstroke instructions.
- Painting engine, or simulator: Receives stroke commands and simulates brush physics, paint mixing, and canvas texture, converting each command into pixel-level changes.
- Observation and feedback layer: Periodically provides the current canvas to the model, allowing it to evaluate the result and decide whether corrections are needed.
These components form a closed loop. The model does not simply output all the code at once and wait for the final result. Instead, it paints, observes, and adjusts continuously, closely resembling the workflow of a human artist.
3. The Painting Engine: Simulating Real Brush Behavior
The main technical challenge in procedural painting is achieving believable brush behavior. A capable engine should model at least the following elements.
- Brush shape and texture: A real brush is not a perfect circle. It consists of many bristles, producing rough edges and uneven widths during a stroke. A common approach is to stamp a collection of randomly distributed “bristle points” along a path while using pressure controls to vary the radius.
- Paint wetness: Wet paint continues to spread across the canvas and mix with the underlying color. A local diffusion algorithm can simulate this blending effect. The amount of paint, or
paint_load, decreases as the stroke becomes longer, reproducing the natural effect of a brush gradually running dry. - Canvas texture: The fabric texture of a canvas affects how paint adheres to its surface. A grayscale height map combined with noise can be used to perturb stroke colors during rendering, creating a granular appearance.
A minimal stroke data structure might look like this:
class Stroke:def __init__(self, color_hex, width_px, points,wetness=0.5, paint_load=1.0, brush_type="round"):self.color = hex_to_rgb(color_hex) # For example, #30346aself.width = width_px # Brush width, such as 90 pixelsself.points = points # List of path control pointsself.wetness = wetness # Paint wetness [0,1]self.paint_load = paint_load # Amount of paint loadedself.brush_type = brush_type # Brush type
One subtle but important detail is that the model may deliberately introduce random movements of a few pixels when generating path points. The reason is straightforward: a human arm cannot draw perfectly straight lines. Completely smooth geometric curves can make an image look mechanical. Intentionally modeling imperfection is therefore an important source of realism.
4. Irreversible Strokes and Iterative Correction
Unlike conventional image-editing software, a procedural painting engine is often deliberately designed to be irreversible. Once a stroke has been placed on the canvas, it cannot be undone. Although this constraint appears restrictive, it more closely reflects real painting and forces the model to adopt more robust strategies:
- Work from large to small: Establish the background colors and major forms with broad strokes before adding details. This prevents the model from focusing on local regions too early.
- Build in layers: Similar to the layered glazing process used in oil painting, the system first handles distant scenery and the background, then the middle-ground subject, and finally highlights and surface textures.
- Correct by covering: When a form is inaccurate, the model does not erase the previous stroke. Instead, it covers and reshapes the area with new strokes, as a human painter would. For example, if the subject’s outline is too thin or cluttered, the model can use a wider stroke to redefine the form and apply environmental colors to reduce unwanted lines.
This process is essentially a visual feedback loop. After each operation, the canvas is rasterized and returned to the model. Based on its visual interpretation, the model selects the next action. This is closely related to the perception–decision–execution cycle used in AI agent systems.
5. Developing a Style: From Imitation to an Independent Artistic Identity
Early procedural painting systems were criticized for excessively imitating well-known artistic styles and producing work with limited originality. One potential improvement is to let the model construct a fictional artist persona with consistent experiences and creative preferences. This persona may include preferred subjects, a recurring color system, and characteristic brush habits.
The model can then use textual feedback from the finished artwork to continuously adjust brush parameters and the behavior of the painting engine.
The value of this approach is that it transforms style from imitation of external works into an explicit, iteratively optimized collection of internal parameters. From an engineering perspective, this is similar to introducing a set of interpretable style hyperparameters into the creative process.
6. Procedural Painting vs. Diffusion Models
| Dimension | Diffusion Models | Procedural Painting |
|---|---|---|
| Generation method | Global denoising and pixel-level convergence | Sequential execution of individual strokes |
| Interpretability | Low, because the process occurs in a latent space | High, because every stroke can be inspected |
| Modification granularity | Requires regeneration or localized redrawing | Allows precise addition or covering of strokes |
| Computational cost | Usually faster for a single image | Can become expensive with a large number of strokes |
| Source of style | Distribution of training data | Explicitly defined and iteratively refined |
These two approaches are not necessarily competitors. Diffusion models are effective for quickly producing highly polished images. Procedural painting is better suited to scenarios that require transparent processes, controllable steps, and interpretable styles, such as painting education, replayable creative workflows, and art research.
7. Practical Implementation Guidance
Developers who want to experiment with procedural painting can follow this path:
- Start with a mainstream 2D graphics library, such as Pillow or Cairo, to implement basic stroke rendering. Focus first on bristle texture and paint mixing.
- Define a concise stroke-command protocol so that the model can issue individual instructions through structured output, such as JSON or generated code.
- Build a feedback loop that renders the canvas, captures a snapshot, and sends it back to the model. Control the number of iterations to balance quality and cost.
- If the system requires compute scheduling and result storage, use general-purpose cloud capabilities such as object storage, container platforms, monitoring, and alerting to build the pipeline. Services provided by platforms such as Baidu AI Cloud can support this type of infrastructure.
8. Conclusion
Procedural painting demonstrates that the value of large language models lies not only in generating results, but also in planning processes. When a model learns to think like a painter—blocking in the background, shaping forms, observing the canvas, and making corrections—AI-assisted creation can move from black-box generation toward a more interpretable and teachable engineering workflow.
This approach is still at an early stage, but its implications for the intersection of AI agents, code generation, and visual understanding are worth watching.
