Painting with Code: AI Models Simulate Brush Physics Stroke by Stroke

    Most AI image generators use diffusion models, which turn random noise into finished images with little visibility into their decisions. This article describes a different approach, in which a model writes a program that drives a simulated painting engine and places thousands of individual brush strokes on a virtual canvas. It covers how the engine models canvas, brushes, and pigment, how the model plans, generates code, and checks its work against the canvas, and what this approach offers in editability, lightweight deployment, and style consistency, along with its trade-offs in efficiency and speed.

    From “Generating Images” to “Writing Painting Programs”

    In recent years, the dominant approach to AI image generation has been diffusion models. These models gradually shape semantic information out of random noise until a finished image emerges. The method is efficient and produces realistic results, but its internal decision process is nearly a black box, which makes fine-grained control over the generation process difficult for creators.

    A very different approach is now emerging. Instead of generating an image directly, the model writes a program. The program calls a simulated painting engine, which paints a work from scratch on a virtual canvas through thousands of separate brush strokes. This process does not rely on an image generation model, and it does not use pre-rendered base images or tracing layers. It depends entirely on the model’s own understanding of spatial relationships, color mixing, and brush physics.

    At its core, this approach turns painting from a pixel-level output problem into a problem of sequential decision-making and code generation. The model does not output the final picture. It outputs a set of executable drawing instructions.

    Technical Principles: How Code Paints Realistic Strokes

    In this approach, two components matter most: the simulated painting engine and the brush model.

    Components of the Simulated Painting Engine

    The engine is not a simulator of the physical world. It is an abstract drawing environment built for the model. It includes at least the following modules:

    • Canvas model: Defines parameters such as canvas size, texture, and absorbency. Canvases with different textures produce markedly different edge bleeding in strokes.
    • Brush model: Parametrically defines brush types, such as round brushes, flat brushes, and dry brushes. Key parameters include brush width, hardness, water content, and pigment load.
    • Pigment physics: Simulates how pigment mixes, layers, and spreads on the canvas while wet. For example, wet pigment merges where strokes overlap, while dry strokes form sharp layered edges.

    The Stroke-by-Stroke Workflow

    When the model receives an instruction such as “paint a shopping cart in a parking lot” or “paint a bird at sunrise,” it does not query an image database. It starts a multi-round, iterative code generation process:

    1. Planning: The model first plans the composition, subject placement, light and shadow relationships, and the rough order of painting.
    2. Code generation: The model breaks the plan into segments of Python code. Each segment covers one stroke or a group of strokes. It includes control points (path points), brush selection (for example, a round brush 90 pixels wide), color values (for example, the color code #30346a), and motion trajectory parameters applied to the brush.
    3. Feedback and correction: After the engine runs the code, the model “observes” the current state of the canvas. The key technical point is that the model can visually access the canvas. A visual encoder captures intermediate states of the canvas and compares them with the model’s target image. This reveals composition errors, color deviations, and messy lines. The model then covers or corrects these problems with new strokes in the next round of code generation.

    This plan-execute-observe-replan loop is the core cycle that distinguishes this approach from traditional generative models.

    Engineering Challenges and Design Details

    Although the principle seems intuitive, building a high-quality programmatic painting system faces many engineering challenges.

    1. Irreversible Strokes and an “Error Tolerance” Strategy

    In real painting, a stroke cannot be undone. In the bird example, each of the 111 strokes was permanent once executed. This requires very precise planning. To handle small errors that cannot be avoided, the model developed a “covering correction” strategy. It anticipates that early strokes may be placed slightly off target, so it deliberately uses wider brushes and thicker paint for the underlayers. This leaves “error tolerance” for the finer strokes that follow.

    2. The Huge Scale of the Parameter Space

    A detailed artwork may require hundreds of thousands of individual strokes. In another experiment, completing a photorealistic painting took about 700,000 strokes. A single program for such a work can run from 1,000 to 2,000 lines. Searching for the best solution in a high-dimensional parameter space (coordinates, pressure, speed, color, and opacity) places very high demands on the model’s context window and reasoning ability.

    3. Simulating the “Imperfection” of Human Painting

    One notable design detail is that when the model draws a straight line, it deliberately adds random wobble of about 7 pixels to the path points. The model explained that this slight jitter simulates a physical trait of human painting: a human arm cannot draw a perfectly straight line. This intentional pursuit of imperfection gives the final image a more lively, hand-drawn quality, rather than the mechanical precision of technical drawing.

    Autonomous Style Development and Feedback Optimization

    This generation method is more than a technical difference. It opens new possibilities for artistic expression.

    Moving Beyond Imitation with a Defined Artist Persona

    In early experiments, the model’s paintings were often criticized for closely imitating the style of a famous painter. To address this limitation, developers had the model design a fictional artist persona. This artist had an invented background, preferred subjects, a characteristic color palette, and a consistent painting style. Instead of simply painting a picture, the model created work as this artist. This required the model to integrate style features at a deeper level rather than copying surface brushwork.

    Training Through Conversational Feedback

    The model improves through a training mechanism that uses text feedback. Developers review the actual results on the canvas and give descriptive comments, such as “the lines on the back are too messy” or “the background lacks wetness.” The model must interpret these vague human descriptions and map them to specific brush parameter adjustments. For example, it might reduce brush hardness by 10% or increase the pigment diffusion coefficient by several times. This fine-tuning through language greatly improved the model’s fine-grained control over painting aesthetics.

    Comparison and Outlook: Why Programmatic Painting Matters

    Compared with mainstream diffusion models, programmatic generation is clearly slower and less efficient at this stage.

    Its distinct value lies in the following areas:

    1. Full editability and control: Because the artwork consists of code and vector paths, users can adjust the coordinates of any path, change the color depth of specific strokes, or delete a layer of strokes. Compared with pixel-space editing, this vector-based editing matches how designers work more closely.
    2. Unified and lightweight tech stack: For some cloud applications, rendering needs only a code interpreter, without deploying a large image inference service. This lowers the barrier to entry for certain scenarios.
    3. Independent and continuous artistic style: The model can maintain a highly stable and distinctive personal style. This style is based on a specific set of physical parameters and drawing logic, and it can carry over to different canvas sizes or resolutions without the style drift common in pixel-level generation.

    Conclusion

    Simulating brush strokes on a canvas one stroke at a time through code may not be the most efficient route to high-fidelity images. However, it reflects respect for and interest in the creative process. It shows that AI can understand not only what a painting looks like, but also how a painting is made. When a model places its first stroke and leaves room for correction, the planning and anticipation it shows go well beyond simple pixel fitting. This process-oriented generation paradigm may find a lasting place in niche areas such as animation production, digital art education, and interactive design tools. As model context windows grow and reasoning improves, the precision and speed of programmatic painting may see further advances.