Simulating Real Brush Painting with Python Code: An Alternative to Image Generation Models

    This article explores a novel AI art technique that bypasses conventional image diffusion models. Instead, a large language model writes Python code to simulate physical brush strokes on a virtual canvas, replicating paint wetness, brush texture, and layered corrections. We break down the core principles, code architecture, self-correcting feedback loop, and the potential for evolving independent artistic styles—offering developers a fresh perspective on code-driven creative simulation.

    Rethinking AI Painting: From Pixel Diffusion to Programmed Strokes

    Most mainstream AI image generators rely on diffusion models that iteratively refine random noise into images. However, a different technical paradigm is gaining traction in the developer community: instead of invoking an image model, the language model directly writes Python code that controls virtual brushes on a canvas, stroke by stroke. This approach can emulate brush textures, paint wetness, canvas grain, and even overlay corrections when mistakes are detected—all without a pre-rendered base image or external image model.

    Each artwork is essentially a Python program of 1,000–2,000 lines, executed by a custom painting engine. The model plans and issues hundreds of independent brush commands, turning “painting” into a physical simulation governed by coordinate transforms and material properties.

    1. Core Principles: Instruction Decomposition and Physical Simulation

    Unlike pixel-level generation, this method breaks the drawing process into a large set of discrete brushstroke instructions that the model plans and executes autonomously. The underlying logic is that the model understands not only the spatial structure of the target object but also the physical behavior of the painting medium. Where diffusion models sample from probability distributions, this code-driven approach converts painting into strict mathematical models: paint absorption into the canvas, friction between bristles and surface, pigment accumulation and blending—all abstracted into computable logic.

    For example, when painting a natural scene, the model is restricted to simulating brush physics, wet paint, and canvas texture. Every stroke is emitted as a coordinate command, and once applied, it cannot be undone—mirroring real painting constraints.

    2. Code Implementation: A Simulated Painting Engine

    The model outputs a series of control instructions. Here is a condensed example of the core engine structure:

    1. class Canvas:
    2. def __init__(self, width, height):
    3. self.width = width
    4. self.height = height
    5. self.pixels = [[(255, 255, 255) for _ in range(width)] for _ in range(height)]
    6. self.wetness_map = [[0.0 for _ in range(width)] for _ in range(height)] # paint wetness
    7. def apply_paint(self, x, y, color, brush_width, wetness):
    8. # Simulate canvas texture and paint diffusion
    9. radius = brush_width // 2
    10. for dy in range(-radius, radius + 1):
    11. for dx in range(-radius, radius + 1):
    12. dist = (dx**2 + dy**2) ** 0.5
    13. if dist <= radius:
    14. px, py = x + dx, y + dy
    15. if 0 <= px < self.width and 0 <= py < self.height:
    16. # Blend color based on wetness and distance falloff
    17. blend_factor = (1 - dist / radius) * wetness
    18. old_color = self.pixels[py][px]
    19. new_r = int(old_color[0] * (1 - blend_factor) + color[0] * blend_factor)
    20. new_g = int(old_color[1] * (1 - blend_factor) + color[1] * blend_factor)
    21. new_b = int(old_color[2] * (1 - blend_factor) + color[2] * blend_factor)
    22. self.pixels[py][px] = (new_r, new_g, new_b)
    23. self.wetness_map[py][px] = max(self.wetness_map[py][px], wetness)
    24. class BrushStroke:
    25. def __init__(self, color, width, points, wetness):
    26. self.color = color
    27. self.width = width
    28. self.points = points
    29. self.wetness = wetness
    30. def render(self, canvas):
    31. for i in range(len(self.points) - 1):
    32. p1, p2 = self.points[i], self.points[i+1]
    33. # Interpolate smooth path and apply paint
    34. steps = int(((p2[0]-p1[0])**2 + (p2[1]-p1[1])**2) ** 0.5)
    35. for s in range(steps + 1):
    36. t = s / max(1, steps)
    37. interp_x = int(p1[0] * (1 - t) + p2[0] * t)
    38. interp_y = int(p1[1] * (1 - t) + p2[1] * t)
    39. canvas.apply_paint(interp_x, interp_y, self.color, self.width, self.wetness)

    In a practical test, a research team asked the model to paint a bird at sunrise. It used 111 strokes in total. The first stroke was defined with color #30346a, a brush width of 90 pixels, and five slightly jittered path points. The deliberate ~7-pixel wobble reflects human arm motion—perfectly straight lines are physically impossible for a human hand. Such realistic details are precisely encoded in the coordinates.

    3. Persona Design and Independent Artistic Styles

    This code-driven approach allows the model to develop its own artistic identity. To add depth, researchers let the model invent a fictional artist persona with a consistent creative style—for example, “an impressionist painter who excels at thick layering to express light and shadow.” Before painting, the model constructs this background, then adapts its brush parameters and the underlying physical engine based on textual feedback.

    For instance, if a reviewer says “color transitions are too harsh,” the model might reduce paint wetness, increase brush hardness, and add intermediate blending strokes in subsequent iterations. This persona-driven process makes the output more than a cold pixel array—it becomes a creative evolution with emotional and stylistic coherence.

    4. Closed-Loop Feedback: Self-Observation and Overlay Corrections

    Perhaps most striking is the model’s ability to observe its own intermediate results and correct mistakes across multiple passes. In typical code execution, a logic error might crash or produce garbage. But here, the model “looks” at the virtual canvas and analyzes what it has painted. If the bird’s back appears too thin or the lines are chaotic, it does not ask the system to undo previous strokes—because physically the paint is already on the canvas. Instead, it plans new strokes that use pigment coverage to mask flaws and adjust structure. This forms a closed loop: generate → observe → analyze → regenerate, much like a real painter layering corrections.

    5. Implications and Outlook

    This technique—simulating real brush painting through Python code without relying on conventional image models—opens new avenues for AI in artistic creation and complex physical simulation. For developers, it highlights the deep potential of large language models in code generation and spatial reasoning. It also suggests that such workloads—tens of thousands of lines of code and millions of brush calculations—can be hosted on general-purpose compute instances and container platforms provided by major cloud vendors, such as those offered by Baidu AI Cloud or similar infrastructure services. As model reasoning improves, code-driven physical simulation may play a significant role in game engines, industrial simulation, and other vertical domains.