Procedural Painting: Simulating the Drawing Process Stroke by Stroke in Code

    Most image generation methods output pixels directly, hiding the creative process. This article explores a different approach: programmatic painting, where the system generates an ordered sequence of drawing instructions—canvas, brush, and strokes—that can be recorded, replayed, and verified step by step. It covers stroke path generation with Bézier curves and hand-tremor noise, paint mixing and wetness modeling, canvas texture, layered rendering with a visual feedback loop, and key engineering practices such as random seeds, stroke logging, and parameter validation. Sample Python code illustrates the core implementation.

    1. Background: From Outputting Pixels to Outputting the Process

    Most mainstream image generation methods are built on diffusion models. These models start from random noise and produce the final pixels through multiple denoising steps. The results are usually satisfactory, but the generation process is implicit: intermediate states are hard to interpret, and precise control over a specific region is difficult.

    An alternative approach treats the painting process itself as the output. Instead of directly producing an image, the program generates an ordered set of drawing instructions—for example, “place a stroke of a given color and width at a given coordinate.” Every instruction can be recorded, replayed, and inspected. In this way, creation is decomposed into an engineering problem that can be verified step by step.

    2. Core Design: The Stroke as the Minimal Unit of Operation

    This type of system typically consists of three kinds of objects:

    • Canvas: stores the color, paint thickness, and wetness state at each pixel;
    • Brush: defines width, pressure curve, texture, and paint load;
    • Stroke: a parameterized path, which is the minimal executable unit.

    One design constraint must be respected: once a stroke lands on the canvas, it becomes part of the historical state and cannot be undone. Corrections can only be made by covering or blending existing content with new strokes. This mirrors real painting and makes every step traceable.

    3. Stroke Paths: Giving Lines a Hand-Drawn Character

    Mathematically perfect lines do not look hand-drawn. In practice, a smooth skeleton is usually generated with a cubic Bézier curve, sampled along its length, and then small random perturbations are applied to the sampled points. The perturbation amplitude should be kept to a small fraction of the brush width, preserving natural jitter without breaking the overall shape.

    1. import math
    2. import random
    3. def cubic_bezier(p0, p1, p2, p3, steps):
    4. points = []
    5. for i in range(steps + 1):
    6. t = i / steps
    7. u = 1 - t
    8. x = u**3 * p0[0] + 3 * u**2 * t * p1[0] + 3 * u * t**2 * p2[0] + t**3 * p3[0]
    9. y = u**3 * p0[1] + 3 * u**2 * t * p1[1] + 3 * u * t**2 * p2[1] + t**3 * p3[1]
    10. points.append((x, y))
    11. return points
    12. def add_hand_tremor(points, amplitude, rng):
    13. return [(x + rng.gauss(0, amplitude), y + rng.gauss(0, amplitude)) for x, y in points]

    The sampling interval must be smaller than the brush radius; otherwise the stroke will show gaps.

    4. Paint and Wetness Modeling

    Paint behavior can be split into two parts:

    1. Color mixing: an approximate subtractive blending calculation. When two colors overlap, the result shifts toward a darker and more saturated direction.
    2. Wetness: when wetness is high, strokes spread more, edges are softer, and blending with existing colors is easier. When wetness is low, edges become dry and tend to leave broken textures.

    Wetness can be modeled as a state variable that decays over time. After each stroke, wetness decreases at a fixed rate and affects the blending coefficient of subsequent strokes. This state design is simple but has a noticeable impact on the visual result.

    The following code shows how a single stroke is deposited onto the canvas. The canvas.blend method must be implemented by the canvas class and is omitted here:

    1. def deposit_stroke(canvas, points, color, width, wetness):
    2. radius = width / 2
    3. spread = radius * (0.5 + wetness)
    4. for x, y in points:
    5. for dy in range(-int(spread), int(spread) + 1):
    6. for dx in range(-int(spread), int(spread) + 1):
    7. px, py = int(x) + dx, int(y) + dy
    8. if 0 <= px < canvas.width and 0 <= py < canvas.height:
    9. dist = math.hypot(dx, dy)
    10. if dist <= spread:
    11. alpha = (1 - dist / spread) * (0.6 + 0.4 * wetness)
    12. canvas.blend(px, py, color, alpha)

    This implementation uses a per-pixel loop for clarity and is therefore slow. In production, it should be replaced with batch computation over the stroke’s bounding box.

    5. Canvas Texture and Brush Granularity

    The canvas itself has texture. A common approach is to represent the texture as a height map. The amount of paint deposited at a position correlates with the height there: raised areas pick up paint first, while recessed areas are left blank. The grain distribution of the brush also creates discontinuities during coverage.

    This layer does not require complex physical simulation. As long as the texture is stable and reproducible on the same canvas, a consistent look can be achieved.

    6. Layered Rendering and the Visual Feedback Loop

    Refining a painting stroke by stroke from the start usually yields poor results. A more robust workflow proceeds in layers:

    1. Layout layer: use a few large strokes to establish light-dark relationships and main contours;
    2. Structure layer: add boundaries and directions of the main forms;
    3. Detail layer: place small strokes in key areas.

    After each stage, the system renders the current canvas, compares it with the target description or reference image, computes pixel differences or structural similarity, locates the regions with the largest deviation, and then plans the next batch of corrective strokes. The loop logic is as follows:

    1. for stage in ["layout", "structure", "detail"]:
    2. for _ in range(max_rounds):
    3. if error_in_stage(canvas, target, stage) < threshold:
    4. break
    5. region = most_different_region(canvas, target)
    6. strokes = plan_corrective_strokes(region, stage, max_strokes=20)
    7. for stroke in strokes:
    8. deposit_stroke(canvas, stroke.points, stroke.color, stroke.width, stroke.wetness)
    9. log_strokes(strokes)

    Because strokes cannot be undone, each round of planning must be conservative. Placing too many strokes at once leaves less room for later correction.

    7. Engineering Practice Notes

    • Random seed: fixing the seed allows the same artwork to be reproduced, which helps with debugging and comparison.
    • Instruction log: save all stroke parameters in a structured format for replay and auditing.
    • Parameter validation: width, wetness, and color values must be constrained to reasonable ranges to prevent abnormal data from corrupting the canvas.
    • Sampling density: the path sampling interval should be smaller than the brush radius.
    • Performance optimization: