How to Simulate Real Brushstrokes with Code Instead of Calling an Image Model

    This article explores an alternative to diffusion-based image generation: having a model write an executable program that simulates a real paintbrush on a virtual canvas, stroke by stroke. It breaks down the core mechanics—stroke instructions, an irreversible painting constraint, multi-round visual self-correction, and the idea of an artist persona as reusable style parameters—then compares this approach with one-shot generation. It also outlines engineering considerations for running such experiments on the cloud and discusses the current limitations. Readers will learn why treating painting as code improves explainability, reproducibility, and iteration.

    1. The Problem: When “Generating an Image” Becomes “Writing a Painting Program”

    In the popular understanding, AI art is almost synonymous with “enter a prompt, get a finished image.” Behind this workflow is usually a diffusion-based generation approach: a model repeatedly denoises random noise in a latent space and finally decodes pixels in a single pass. This path is end-to-end and fast, but the process is a black box to the user. Every line in the image is not “painted” but “computed.”

    The source material describes a clearly different approach: the model does not output an image directly. Instead, it writes a program that simulates the movement of a real paintbrush on a virtual canvas, completing the work stroke by stroke, with the ability to simulate brush texture, paint wetness, and canvas grain. When it finds a mistake, it covers and corrects it with new strokes. In other words, the output is not an image but an executable, reviewable, and reproducible source code for painting.

    This difference brings three engineering benefits:

    • Explainability: every stroke is an explicit instruction containing parameters such as coordinates, color, brush width, and path points;
    • Reproducibility: the same code produces the same result under the same rendering engine;
    • Iterability: a correction is not “resampling” but appending strokes or adjusting parameters and re-running.

    2. Core Mechanics: From Stroke Instructions to the Canvas

    1. A stroke is decomposed into many independent instructions

    The model needs to break down “paint a bird” into hundreds or thousands of independent stroke instructions. In the sunrise bird example from the source material, the entire painting uses only 111 strokes. The first stroke uses a dark blue color, a brush width of about 90 pixels, and 5 path points with slight wobble. The model explains that the roughly 7-pixel wobble is intentional, because a human arm cannot draw a perfectly straight line either. This shows that stroke parameters are not random; they carry an intent to model real physical behavior.

    A schematic instruction structure is shown below (it only expresses the data shape, not a specific product interface):

    1. stroke = {
    2. "color": "#30346a",
    3. "brush_width": 90,
    4. "points": [(x0, y0), (x1, y1), ...], # 5 path points with slight jitter
    5. "jitter_px": 7,
    6. "wetness": 0.6, # paint wetness, affects edge diffusion
    7. "canvas": "weave" # canvas texture type
    8. }

    2. An irreversible constraint raises the planning difficulty

    In another experiment, the model is required to simulate only the physical properties of the brush, wet paint, and canvas texture through code, and each stroke must be output separately as a coordinate instruction and cannot be undone once placed. This constraint turns painting from “reversible editing” into “one-shot execution,” forcing the model to anticipate composition, occlusion relationships, and light-and-dark layers before placing a stroke. It also explains why the model needs multiple rounds to observe the canvas and check its own mistakes.

    3. Multi-round visual self-checking and correction

    The source material mentions that when the bird’s back is drawn too thin and the lines look messy, the model observes the current canvas and adjusts subsequent strokes. In engineering terms, this forms a “generate–render–review–correct” loop: after the program executes and produces an intermediate image, the model reads that image, judges the deviation, and then appends or rewrites stroke parameters. Compared with traditional one-shot generation, this is closer to how a human painter “steps back to look at the whole.”

    4. Code size and “artistic persona”

    According to the source material, this kind of work is essentially a program written by the model, usually 1,000 to 2,000 lines of code, controlling different types of brushes through a simulated painting engine. To prevent the work from excessively imitating existing styles, one approach is to let the model set up an artist persona with a fictional background and a stable creative style, then guide it to adjust brush and engine parameters through textual feedback based on the visual result. The key here is not the role-playing itself, but fixing style constraints as reusable parameter priors.

    3. Comparison with the Traditional Generation Path

    Dimension Diffusion-based one-shot generation Code-simulated brush painting
    Output Pixel image Executable program + rendered result
    Process visibility Low High, traceable stroke by stroke
    Correction method Resampling or local inpainting Appending strokes, changing parameters and re-running
    Style control Relies on prompts and model priors Persona and parameter constraints can be written in
    Compute profile Intensive during inference Alternating inference and rendering, longer pipeline

    It should be emphasized that the two paths are not replacements for each other. The former is suitable for fast output, while the latter is suitable for studying the painting process itself and for scenarios that require strongly explainable results.

    4. Engineering Considerations for Running Such Experiments on the Cloud

    To reproduce a similar experiment on Baidu AI Cloud, you can organize the pipeline around general cloud product categories:

    • A container platform hosts the painting engine and rendering tasks, scaling elastically by stroke batch;
    • Object storage saves code versions, intermediate canvas snapshots, and the final work for traceability;
    • A message queue decouples “model instruction generation” from “engine rendering execution” to avoid long tasks blocking the pipeline;
    • A logging service records each stroke’s parameters and elapsed time to support error localization;
    • Monitoring and alerting track task success rate and queue backlog to prevent batch experiments from getting out of control.

    On the model side, a layered strategy of “produce a small sample first, then enlarge the details” can be used: determine composition and light-and-dark at low resolution, then increase stroke density region by region, which can significantly shorten a single iteration cycle.

    5. Limitations and Outlook

    The bottlenecks of this paradigm concentrate in three areas: first, the latency and cost brought by the long pipeline; second, the planning error rate under the irreversible constraint; third, the lack of a unified standard for quantitatively evaluating style stability. Future directions to explore include structured reuse of stroke parameters, differentiable evaluation of rendering results, and abstracting the “artistic persona” into a versionable configuration asset. Overall, reducing painting to a program turns the creative process from a black box into a reviewable execution trace, which is the most valuable takeaway from this practice.