How LLMs Paint with Code: Stroke-by-Stroke Image Generation Without Diffusion Models
Most AI image generators today are built on diffusion models, which denoise an entire latent canvas in one pass. This article looks at a different approach: the model writes a drawing program, then drives a painting engine to build the image one stroke at a time. You will see how a single stroke is modeled as a structured instruction, how the rendering engine simulates paint and canvas behavior, why irreversible strokes force a perception–action loop, how a fictional artist persona keeps style consistent, and what the approach costs to run in production.
1. Two Different Paradigms of Image Generation
Almost every mainstream AI image generation approach today is built on diffusion models. The core logic is straightforward: start from a random noise tensor, then let the model repeatedly predict and remove noise over multiple iterations until a complete image “develops.” This is a holistic generation strategy. The final pixel matrix is shaped in latent space in one go, and every intermediate step serves the same target image.
A very different technical path has recently emerged, which reframes image generation as a sequential decision problem. The large model does not output pixels directly. Instead, it first writes a drawing program, then drives a simulated painting engine that accumulates marks on the canvas stroke by stroke, the way a human painter works. One paradigm develops an image out of nothing in a single pass; the other starts from an empty canvas and progressively fills it. The two rest on fundamentally different worldviews.
The value of this path lies in decomposing painting into a large number of independent, explainable, reproducible, and traceable actions. The generation process itself becomes transparent and controllable, and the model has room to develop painting habits and a style reminiscent of a human artist.
2. Core Modeling: A Stroke Is an Instruction
The key abstraction in programmatic painting is formalizing “one stroke” as a structured instruction. Each stroke carries at least the following parameters:
- Start and end coordinates, plus a sequence of path points: a stroke is rarely a straight line, but a curve formed by several slightly perturbed waypoints;
- Color: expressed as a hexadecimal code or RGB components, such as
#30346a; - Brush width: determines how many pixels the stroke covers, for example 90 pixels;
- Paint wetness: affects how the stroke edges diffuse and how colors blend;
- Brush type: round, flat, dry, wet, and other brushes with different physical characteristics.
One frequently overlooked detail that adds a great deal of realism is path jitter. When the model draws a line, it deliberately introduces a small offset of roughly 7 pixels at the path points. The reason: a human wrist cannot draw perfectly straight lines, so if the model output flawless geometric strokes, the result would look mechanical and artificial instead. Jitter is a form of reverse-engineered humanity.
Illustrative code:
import randomclass Brush:def __init__(self, color, width, wetness=0.5, jitter=7):self.color = color # e.g. "#30346a"self.width = width # brush width in pixelsself.wetness = wetness # paint wetness, affects edge diffusionself.jitter = jitter # wrist jitter, simulates hand-drawn imperfectiondef make_path(self, start, end, points=5):"""Generate path points between start and end with slight perturbation"""path = []for i in range(points):t = i / (points - 1)x = start[0] + (end[0] - start[0]) * ty = start[1] + (end[1] - start[1]) * tpath.append((x + random.uniform(-self.jitter, self.jitter),y + random.uniform(-self.jitter, self.jitter)))return path
3. The Painting Engine: Simulating Physical Behavior
For a program to produce something that looks like a painting, the engine has to simulate real interactions between paint and canvas. This spans at least three layers.
1. Brush and stroke rendering. Different brushes determine the shape of the mark. A round brush suits soft transitions, a flat brush produces sharp edges, and a dry brush (low paint load) leaves broken, streaky passages where the raised canvas texture catches the pigment.
2. A paint mixing model. When wet paint is applied over existing paint, the edges blend naturally instead of overwriting it. In engineering practice, alpha-based linear blending is commonly used, and the higher the wetness, the softer the transition between old and new colors:
def blend(base, top, alpha):"""Blend two color layers by ratio to simulate wet-on-wet overpainting"""if base is None:return topreturn tuple(int(base[i] * (1 - alpha) + top[i] * alpha)for i in range(3))
3. Canvas texture. A canvas is not an ideal flat plane, and its fiber texture affects how paint adheres and spreads. The engine can read a local height field of the canvas before laying down a stroke, applying slight deformation and occlusion to the mark so that weave, grain, and similar textures emerge.
One constraint is decisive here: a stroke cannot be undone once it lands. This mirrors the physics of a real canvas, where dried paint cannot be wiped away. Any correction has to be made by layering new strokes on top, which means the model must plan thoroughly before committing.
4. Self-Observation and the Correction Loop
Precisely because strokes cannot be undone, programmatic painting depends on a perception–action loop:
- The model plans and emits a batch of stroke instructions;
- The engine renders those strokes onto the canvas;
- The system renders the current canvas to an image and feeds it back so the model can “look” at it;
- The model evaluates the result, plans new strokes to correct any deviation, and otherwise proceeds to the next step.
A typical scenario: while painting a bird, the model notices that the lines on the bird’s back are too thin and look messy. It does not undo anything. Instead, it identifies the problem area and re-draws it with a thicker brush and a more suitable color. The whole process is essentially a multi-turn dialogue between the model and the canvas, where each turn narrows the gap between the current picture and the intended target.
The process can be abstracted as a controlled loop:
canvas = Canvas(width, height)target_intent = "a bird at sunrise"while not model.is_satisfied(canvas):observation = canvas.render_for_review() # render for reviewstrokes = model.plan_strokes(observation, target_intent)for stroke in strokes:canvas.apply_stroke(stroke) # a landed stroke is irreversible# if unsatisfied, the next iteration layers new corrective strokes
5. From Imitating Styles to an Independent Artistic Persona
Early on, model-generated artwork was often criticized for over-imitating existing masters’ styles, essentially stitching together familiar expressions from training data. The improvement direction is to have the model play an artist persona with a fictional background and stable working habits: give it a backstory, aesthetic preferences, a signature palette, and characteristic brush behavior, then let humans steer its brush and engine parameters through natural-language feedback such as “that stroke is too heavy” or “the colors run cold.”
With this design, style becomes an internally consistent variable inside the model rather than a copy of an external template, and the work gradually acquires recognizable, continuous personal characteristics.
6. Engineering Considerations for Production
Taking this approach to production raises a few practical questions:
- Stroke count and performance: work aiming for photorealistic fidelity may require hundreds of thousands or even millions of strokes, and the drawing program often runs to thousands of lines of code. The rendering pipeline needs incremental updates and parallelization to avoid the performance bottleneck of repainting the entire canvas.
- State management and reproducibility: record the full parameter sequence for every stroke so the painting process can be replayed and reproduced, which simplifies debugging and style transfer.
- Artifact and pipeline hosting: the generated drawing programs and stroke logs can be stored in object storage, execution details and call traces written to a logging service, and rendering duration, failure rate, and resource consumption tracked with monitoring and alerting capabilities, forming an observable and operable end-to-end pipeline.
- Applicability boundaries: programmatic painting is good at producing work with visible brush texture, a stylized look, and an explainable process, but for hyperrealistic photographic detail and complex lighting physics it may still be less efficient than a diffusion model. The two are not substitutes; they are technical options that can be combined per use case.
7. Conclusion
Letting a large model “hold the brush in code” turns image generation from a black-box, one-shot development into a transparent, explainable, incrementally built creative process. Strokes are instructions, a landed stroke cannot be undone, and the model looks back and corrects. This mechanism reproduces the core experience of human painting while opening a path for AI creative generation that balances control with artistic expression. For developers, the key to this approach is not calling some ready-made API, but designing the physics of the painting engine and the perception–action loop well.
