Code-Driven Painting: Simulating Real Brushwork with Python
Code-driven painting uses executable programs rather than direct image generation to build artwork stroke by stroke. This article examines the architecture, brushstroke parameters, irreversible canvas updates, visual feedback, and engineering trade-offs involved in using Python to simulate brushes, paint, and canvas state. It also outlines a practical implementation path and the approach’s strengths and limitations.
Conventional text-to-image systems produce pixels directly through an image generation model, whether through an end-to-end mapping or repeated refinement of random noise. An alternative treats painting as a procedural process: instead of generating an image directly, a model writes an executable program that controls a virtual canvas, brushes, and paint, gradually building the artwork through individual strokes.
In public demonstrations, a general-purpose large language model has shown this capability by writing Python programs for individual paintings. These programs can run to thousands of lines and control different brushes through a simulated painting engine, rather than calling an external image generation model to produce the finished work.
The main value of this approach is not replacing image generation models. It is making the painting process explicit, inspectable, and open to intervention. The following sections examine its architecture, stroke instructions, irreversible updates, feedback mechanisms, and implementation challenges.
1. From Generating Pixels to Generating a Painting Process
Conventional image generation focuses on the final pixel output. How a model organizes composition, materials, and lighting is often hidden. Code-driven painting instead produces a program containing parameters such as brush type, color, width, path points, wetness, and canvas texture. A rendering engine executes those instructions on the canvas.
This shift introduces three important differences:
- Greater interpretability: Every stroke is an explicit instruction with traceable colors, coordinates, widths, and paths. Developers can investigate problems at the level of individual strokes or planning decisions.
- More control over the process: A painting can begin with broad color areas, move on to light and shadow, and finish with details. A stroke budget can also constrain computational cost.
- A different correction strategy: Once a stroke reaches the canvas, it cannot be undone. The model must correct mistakes through additional strokes that cover, layer over, or refine earlier marks—closer to working with physical paint.
In the public examples described, a painting of shopping carts in a parking lot used approximately 700,000 simulated strokes. Another, depicting a bird at sunrise, used just 111 strokes, with a video showing its progression from a blank canvas to the finished image. This wide range illustrates why stroke planning must adapt to the desired detail, brush size, and task constraints.
2. System Architecture: A Planning, Execution, and Observation Loop
A practical code-driven painting system typically has four layers:
- Goal interpretation and style planning: Breaks the intended subject into composition, layers, lighting, materials, and stroke strategies.
- Stroke planner: Generates stroke sequences while managing the brush library, color space, coverage, and stroke budget.
- Painting engine: Maintains canvas state, including color, wetness, and texture, and applies and blends strokes along specified paths.
- Observation and evaluation: Inspects the current canvas for proportions, tonal relationships, edges, and visual clutter, then provides textual feedback or parameter recommendations.
The following schematic illustrates these components:
@dataclassclass Stroke:points: list[tuple[float, float]]color: strwidth_px: floatwetness: floatbrush_texture: strclass Canvas:def apply(self, stroke: Stroke) -> None:# Irreversible: directly updates canvas color, wetness, and texture....def snapshot(self):# For observation and analysis only, not for rolling back strokes....class Planner:def plan(self, goal, feedback=None) -> list[Stroke]:...class Observer:def critique(self, canvas, goal) -> str:...for stroke in planner.plan(goal):canvas.apply(stroke)feedback = observer.critique(canvas, goal)planner.adjust(feedback)
The essential idea is to observe after execution. Rather than committing to an entire painting without further inspection, the model uses canvas feedback to adjust subsequent planning. The code above is schematic; a working implementation must ensure that planner adjustments actually affect the strokes executed next.
For example, when a bird’s back appears too thin or its lines become cluttered, the model can inspect the canvas, identify the problem, and add strokes to broaden the shape or organize the surrounding marks.
3. Modeling Brushstrokes as Executable Instructions
A stroke instruction must describe at least where to paint, which color to use, its width and wetness, the brush texture, and how its path changes.
In one public example, the first stroke used the color #30346a, a width of 90 pixels, and five slightly wavering path points, with approximately 7 pixels of intended wobble. This variation was not merely random noise: it was designed to reflect the fact that a human arm does not produce perfectly straight lines.
For implementation purposes, stroke parameters can be organized into four groups:
- Geometry: Path points, interpolation method, and pressure at the beginning and end of a stroke.
- Appearance: Color, opacity, width, and edge hardness.
- Material behavior: Wetness, diffusion, and canvas texture sampling.
- Strategy: Brush type, application order, and stroke budget.
Planning Under an Irreversible Constraint
Irreversibility makes planning substantially harder. Conventional digital painting allows undo, erasing, or rollback. In this approach, marks can only be followed by additional marks.
The planner therefore needs to anticipate later work: broad strokes before fine ones, lighter areas before darker ones, and overall structure before local detail. Early, wide strokes must avoid damaging regions that will matter later.
Corrections should likewise rely on covering strokes and fine-stroke repairs rather than erasing.
4. Visual Feedback and a Consistent Artistic Persona
A stable style requires more than a single prompt. One approach described in public practice is to have the model create an “artist agent” with a fictional background and a consistent creative style, then use textual feedback to guide adjustments to brushes and the painting engine.
The purpose is to encourage internally consistent color preferences, brushwork, and compositional choices while reducing direct imitation of existing artistic styles.
The evaluator needs to inspect the work at multiple scales:
- Is the overall composition balanced?
- Are the subject’s proportions accurate?
- Do the light and dark areas form a coherent structure?
- Are edges becoming cluttered?
- Has detail become excessive?
Feedback can take the form of written criticism or parameter suggestions, such as widening the brush in a particular area, reducing wetness, or increasing the density of fine strokes.
Evaluator bias can cause the planner to over-optimize isolated details. Rule-based checks and repeated feedback should therefore complement visual critique.
5. Engineering Challenges and Optimization Priorities
The main implementation challenges include the following:
- High computational cost: A painting with 700,000 strokes requires substantial rasterization and state updating. Incremental rendering, region clipping, and stroke merging are important optimization directions.
- Complex state management: A canvas may store not only color but also wetness, texture, and brush coverage. These channels must remain consistent as strokes are applied.
- Long-sequence planning: Programs spanning thousands of lines need a modular structure to reduce non-executable code and conflicting parameters.
- Difficult evaluation: Final pixel similarity is insufficient on its own. Evaluation must also consider the coherence of the process and the quality of individual strokes.
- Limited parallelism under irreversible updates: Stroke order matters. Planning should account for regions and layers, with explicit stroke budgets and inspection checkpoints.
A practical development path starts with simple targets:
- Implement the canvas and brush interfaces, ensuring that
applyis irreversible. - Add the stroke data structure and a basic planner.
- Introduce an observer and test the feedback loop on simple geometric shapes.
- Gradually add material behavior, wetness, and style constraints.
Throughout development, retain a stroke log for debugging and reproducibility. Under the irreversible painting constraint, checkpoints are for inspection rather than undoing marks.
6. Suitable Use Cases and Limitations
Code-driven painting is well suited to applications that prioritize process control, interpretability, stylization, or educational demonstrations. It is also relevant to procedural art and visualization research.
The approach described here does not call an image generation model or rely on a pre-rendered image or tracing underlay, making each step available for inspection. Those properties distinguish this workflow; they are not guarantees of every system that generates painting code.
Its computational cost and planning complexity make it less suitable for very low-latency, large-scale image production. Conventional image generation remains useful for producing images quickly, while code-driven painting is better aligned with tasks where the process, constraints, and interpretability matter.
Ultimately, this approach reframes image creation as the generation of a painting program. Python code simulates brushes, paint, and canvas state, while stroke sequences, irreversible updates, and visual feedback guide the work toward completion. For developers, it offers an alternative technical path for procedural art and interpretable generation—one in which the construction process is as inspectable as the final image.
