Code-Driven Stroke Painting: Simulating Brushes in Code and the Technical Principles Behind Them

    This article explains how a model can produce a painting by writing programs that place brush strokes on a virtual canvas, with a renderer forming the final image. It compares this approach with direct image generation, describes how strokes and paint effects can be represented, outlines a staged planning strategy, and covers feedback, reliability, evaluation, and the technical limits of the method.

    1. From Generating Images to Generating the Painting Process

    A model does not have to output a finished image directly to complete a painting. Another approach has the model first plan a painting strategy, then write a program that uses a virtual brush to lay down colors stroke by stroke on a canvas. A rendering engine turns those strokes into the final image.

    The key to this approach is not splitting an existing picture into strokes. It is representing the creative process as a sequence of executable operations. For example, the system might first block in the sky and ground with a wide brush, outline the main subject with a finer brush, and finally add highlights, shadows, and local texture.

    The reference material describes two experiments. One uses a large number of simulated strokes to depict a shopping cart in a parking lot. The other draws birds in a sunrise scene under a constraint: once a stroke is placed, it cannot be undone. The two experiments have different goals. The first emphasizes structural complexity and detail density. The second emphasizes composition planning and coverage strategy under a limited number of strokes.

    Demonstration results, however, should not be taken as general capability. Completing one painting does not mean the system will reach the same quality for any subject, resolution, or compute budget. A more useful way to analyze this method is to understand its representation, execution mechanism, and engineering boundaries.

    2. How It Differs from Direct Image Generation

    Common diffusion-based image generation builds an image through iterative denoising. That process happens in pixel space or a latent representation. Code-driven stroke painting instead makes the intermediate process explicit as brush instructions and canvas state changes.

    The basic pipeline can be summarized as:

    Text request → Scene planning → Painting program → Stroke execution → Canvas update → Result inspection

    The main difference between the two approaches lies in what the system chooses as its operable intermediate representation.

    Direct image generation usually models the overall visual distribution. Stroke painting must answer specific questions: what to paint, where to start, which color to use, how wide the brush should be, how the path changes, and what order the strokes overlap in.

    This explicit representation has three main benefits.

    First, it is replayable. If the system saves the initial canvas, the stroke sequence, the renderer configuration, and related random states, it has a chance to reproduce the painting process.

    Second, it is constrainable. The system can limit the number of strokes, available colors, brush types, and coverage rules so that the creative process meets specified requirements.

    Third, it is inspectable. Developers can identify which step broke an outline or which group of strokes caused color contamination, instead of guessing from the final image alone.

    Explicit representation does not automatically mean higher quality. Fine text, repeated structures, complex perspective, and realistic materials may still require many strokes and multiple rounds of inspection.

    3. Define the Stroke First, Then Plan Intelligently

    Stroke-by-stroke painting requires a stable stroke data structure first. Without one, the model outputs only a vague description of a painting, which cannot be executed reliably.

    A basic stroke typically includes the following fields:

    • Path: a set of positions describing the brush’s movement.
    • Color: the paint color used for this stroke.
    • Width: the brush’s coverage. It can be fixed or vary along the path.
    • Pressure: adjusts width, coverage, or paint deposit.
    • Opacity: controls how the stroke blends with the existing image.
    • Brush type: for example, round, flat, or textured brushes.
    • Wetness state: when wet-paint simulation is supported, used in diffusion, dragging, and color mixing.

    Not every system needs all of these fields. A first version only needs path, color, width, and opacity to validate the basic workflow. Pressure variation, wet mixing, and texture interaction can be added gradually.

    Paths also cannot be formed by connecting sparse coordinate points directly. If adjacent sample positions are too far apart, the stroke breaks. A renderer usually interpolates along the path, places brush marks continuously, and adjusts sampling density based on the degree of turn, brush width, and speed.

    Count strokes and path points separately. One long stroke can contain many path points, and counting it as a single stroke does not change its actual computational cost.

    4. How the Virtual Brush Produces Visible Paint Effects

    4.1 Brush Marks Determine Local Shape

    The simplest brush can be modeled as a circular coverage area with soft edges. Repeatedly stamping these areas along a path forms a continuous line.

    To simulate different textures, the system can change the mark’s shape, edge falloff, angle, internal grain distribution, and transparency. However, adding random perturbation does not equal reproducing a real brush. Perturbation must be constrained by scale and direction. Otherwise, brush texture turns into random noise.

    For flat brushes, orientation matters. The same path at different angles creates different edges and coverage widths.

    4.2 Alpha Compositing Is Not Real Color Mixing

    A basic painting system can use alpha compositing. If the foreground color is (C_f), the existing canvas color is (C_b), and the coverage ratio is (\alpha), a simple composite can be written as:

    [
    C_{out}=\alpha C_f+(1-\alpha)C_b
    ]

    A real implementation should also pay attention to color space handling. Computing directly on nonlinearly encoded color values can produce unreasonable light and dark results.

    More importantly, this formula describes color compositing, not a complete pigment physics model. Real pigments involve absorption, scattering, concentration, and the medium, so they cannot be equated with averaging two color values.

    Engineering documentation should therefore clearly distinguish visual approximation from physical simulation. If a system only implements alpha coverage, it should not claim to accurately reproduce real paint behavior.

    4.3 Wet Effects Depend on Persistent State

    Representing wet painting requires more than a “wetness” field on each stroke. The canvas needs to store state that changes over time, such as wetness level, paint distribution, and local drying status.

    When a new stroke touches a wet area, the system can trigger color mixing or diffusion within a limited range. When it lands on a dry area, it mainly appears as coverage.