Painting with Code, Stroke by Stroke: A New Paradigm for Image Generation with LLMs
Mainstream AI image generation relies on diffusion models that produce a complete bitmap from a text prompt through an opaque denoising process. A new paradigm takes a different route: instead of calling an image generation model, a large language model writes a program that simulates real brushstrokes—brush physics, paint wetness, canvas texture—and paints step by step like a human artist. This article breaks down the three-layer architecture behind simulated painting engines (planning, simulation, and feedback), illustrates the stroke instruction data structure and engine logic with example code, and discusses the paradigm's unique value in interpretability, process-as-asset, style independence, and as a probe for general intelligence, along with practical engineering challenges and advice for developers who want to experiment with it.
Painting with Code, Stroke by Stroke: A New Paradigm for Image Generation with LLMs
1. From “All at Once” to “Stroke by Stroke”: A Divide in Generation Paradigms
Most mainstream AI image generation approaches today are built on diffusion models: starting from random noise, they iteratively denoise toward a complete image. The input is a text prompt, the output is a bitmap, and the intermediate process is essentially a black box to the user.
A fundamentally different approach has emerged recently: instead of calling any image generation model, a large language model directly writes a program that simulates the motion of a real brush on a virtual canvas—stroke trajectories, paint wetness, brush texture, and canvas grain—completing the artwork stroke by stroke, much like a human painter. In public demonstrations, one model produced near-photorealistic imagery with roughly 700,000 simulated strokes; another experiment finished a well-composed sunrise scene with flying birds using only 111 strokes and a program of about a thousand lines.
This paradigm deserves developers’ attention because it reframes “generate an image” as “plan and execute a painting program”—which happens to be exactly what large language models are best at.
2. Architecture Breakdown: How a Simulated Painting Engine Works
The overall process can be abstracted into three layers:
1. Planning Layer (the Large Language Model)
The model makes the “artistic decisions”: how to arrange the composition, whether to paint the background or the subject first, what color each stroke uses, how wide the brush is, and what path it follows. These decisions are ultimately serialized into structured stroke instructions.
2. Simulation Layer (the Painting Engine)
The engine “materializes” stroke instructions into pixels. The core physical properties it must simulate include:
- Brush model: brush shape, width, edge softness, and bristle texture;
- Paint model: color mixing (subtractive mixing rather than simple RGB addition), paint wetness, opacity, and coverage;
- Canvas model: base color and how grain texture affects paint adhesion;
- Motion model: pressure variation and natural path jitter—human arms can’t draw perfectly straight lines, so in the demonstrations the model deliberately adds about 7 pixels of random sway to paths, which actually makes the lines look more realistic.
3. Feedback Layer (Self-Inspection and Correction)
This is the biggest difference from traditional procedural drawing. Because strokes cannot be undone, the model must “observe” the current canvas state during painting, check for errors, and correct them with new covering strokes. For example, when painting a bird, if the model finds the back line too thin and the outline messy, it will proactively add strokes to thicken the form and unify the color tone. This “observe—evaluate—correct” loop essentially ports the debugging cycle from the code domain into visual creation.
3. Illustrative Implementation of Stroke Instructions and the Engine
The simplified example code below helps illustrate the stroke instruction data structure and the engine’s basic rendering logic:
from dataclasses import dataclassfrom typing import List, Tuple@dataclassclass Stroke:color: str # paint color code, e.g. "#30346a"brush_width: int # brush width (pixels)path: List[Tuple[int, int]] # sequence of path pointswetness: float # paint wetness, 0~1pressure: float # stroke pressure, 0~1jitter: float = 7.0 # deliberately retained path jitter amplitudeclass PaintingEngine:def __init__(self, width, height, canvas_texture="fine_grain"):self.canvas = self._init_canvas(width, height, canvas_texture)def apply_stroke(self, stroke: Stroke):# 1. Interpolate along the path with natural jitter appliedpoints = self._interpolate(stroke.path, jitter=stroke.jitter)# 2. Generate paint footprints based on brush texture and pressurefor x, y in points:footprint = self._brush_footprint(stroke.brush_width, stroke.pressure, x, y)# 3. Blend colors based on wetness and existing paint on canvasself._blend(footprint, stroke.color, stroke.wetness)# Note: there is no undo; once a stroke lands it is permanentdef snapshot(self):"""Lets the model 'observe' the current canvas to drive the next decision"""return self.canvas.copy()
The key design point is the snapshot interface: after every few strokes, the model obtains a canvas snapshot for visual evaluation, then decides its next action. Structurally, this is isomorphic to the “state–action” loop in reinforcement learning.
4. The Unique Value of This Paradigm
Interpretability and auditability: every artwork is a complete, readable program. Any color patch in the image can be traced back to a specific stroke instruction and its decision context. By contrast, the generation process of diffusion models is almost impossible to attribute pixel by pixel.
The process itself is an asset: the painting process is inherently time-series data that can be recorded, replayed, and edited, making it naturally suited for producing time-lapse painting videos—directly valuable in teaching and content creation scenarios.
Independent evolution of style: a diffusion model’s style depends heavily on its training data distribution and tends to drift toward imitating existing artists’ styles. In code-driven creation, you can assign the model a fictional “artist persona”—including its imagined background and stable aesthetic preferences—and then continuously fine-tune engine parameters through textual feedback, allowing the model to gradually develop a relatively independent, reproducible personal style. This mitigates, to some extent, the copyright and style disputes surrounding AI art.
A probe of capability boundaries: whether a model can plan a globally coherent image from discrete strokes is a comprehensive test of long-horizon planning, spatial reasoning, and self-correction—so this kind of task is also regarded as a new benchmark for evaluating a model’s general intelligence.
5. Engineering Challenges and Practical Advice
That said, this paradigm currently has clear constraints:
- High cost: photorealistic works require hundreds of thousands to millions of strokes, and the corresponding inference and rendering overhead far exceeds a single diffusion sampling run;
- Engine quality determines the ceiling: the fidelity of the color-mixing model and brush physics directly determines the final texture, and the engine itself is a non-trivial R&D investment;
- Error accumulation: since strokes cannot be undone, early mistakes can only be covered up later, placing extremely high demands on the model’s global planning ability.
For developers who want to try this, it is advisable to start with low-resolution canvases and coarse-grained brushes—first getting the full “instruction—render—snapshot—feedback” loop working end to end, then gradually increasing the engine’s physical fidelity. On the infrastructure side, rendering tasks can be elastically scheduled on a container platform, canvas snapshots and process data can be stored in object storage, task state can be decoupled through message queues, and monitoring and alerting can track the health of long-running tasks—all standard patterns that general-purpose cloud services can support.
6. Conclusion
“Simulating brushes with code” is not meant to replace diffusion models; it opens a complementary route. Diffusion models win on efficiency and ease of use, while the stroke-by-stroke approach wins on controllability, interpretability, and the value of the process itself. As large models’ planning and self-correction capabilities continue to evolve, “AI painting by writing programs” is poised to move from flashy demos to practical scenarios such as design assistance, art education, and generative content production. For developers, now is a good time to understand this paradigm and start experimenting with it hands-on.
