LLM-Driven Painting: Using Code to Simulate Physical Brushstrokes
Code-driven painting uses large language models to write programs that build images stroke by stroke rather than generate them directly with an image model. This article examines virtual brush mechanics, paint–canvas interactions, feedback-based corrections, and artistic style development, along with implications for automated workflows, cloud deployment, and precise control.
Diffusion models dominate today’s AI image-generation landscape, producing images by progressively denoising random noise. Although this approach delivers compelling visual results, its largely opaque generation process can make precise control over individual details difficult.
An alternative explored in the developer community puts code at the center of image creation. Instead of calling a conventional image-generation model, a large language model writes a program that simulates brush movements on a virtual canvas, building a painting one stroke at a time.
This approach extends the possibilities of AI-generated content while illustrating how LLMs can decompose complex tasks, generate executable instructions, and refine results through feedback.
1. Diffusion Models vs. Code-Driven Painting
Diffusion-based image-generation tools start with random noise and progressively recover image features through denoising. They can produce striking results quickly, but precise local control, process transparency, and incremental construction remain challenges.
Code-driven painting takes a different approach. It breaks a complete painting into individual brushstroke instructions that an LLM plans and implements through code. Rather than directly producing a pixel array, the model generates a Python program that may span 1,000–2,000 lines.
That program acts as a virtual painting engine. It controls brush types, paint properties, and canvas parameters to construct the final image stroke by stroke.
The key shift is from direct image synthesis to programmable construction. Artistic decisions become executable operations, making the rendering sequence inspectable and allowing developers to trace how individual steps contribute to the result.
2. Building a Virtual Physical Painting Engine
Convincing simulated painting depends on modeling interactions among the brush, paint, and canvas. The generated Python code must represent enough of these physical behaviors to create believable marks and textures.
Simulating Brush Mechanics
A real brush deforms as it touches a surface. Pressure varies across its bristles, and the painter’s movements introduce small deviations.
In a code-driven system, each stroke can be represented by coordinates together with parameters such as brush width, color, and path points. When laying down a base color, for example, the program might select a particular color value, use a broad brush, and define a path with slight variations.
A maximum deviation of around seven pixels can introduce deliberate irregularity. The purpose is to avoid mechanically perfect lines: small imperfections help suggest the natural variability of a hand-guided brush.
Modeling Paint–Canvas Interactions
In oil and watercolor painting, paint wetness affects blending and bleeding. Canvas texture also influences how pigment is deposited, producing characteristic surface patterns.
A simulated painting engine can use simplified fluid-dynamics calculations or noise-based texture mapping to represent paint spreading and layering over a textured surface.
The result of a stroke therefore depends not only on its current parameters but also on the paint already present. Earlier strokes influence subsequent ones through their color, coverage, and wetness. These state-dependent interactions increase computational complexity while giving the image a stronger sense of physical depth.
The following simplified Python-style pseudocode illustrates the core structure:
class VirtualBrush:def __init__(self, color, width, wetness):self.color = color # For example, '#30346a'self.width = width # Width in pixels, such as 90self.wetness = wetness # Paint wetnessdef stroke(self, canvas, start_point, end_point, human_jitter=True):path_points = calculate_path(start_point, end_point)if human_jitter:# Introduce small deviations to approximate hand-guided motionpath_points = add_micro_jitter(path_points, max_deviation=7)for point in path_points:# Compute interactions with canvas texture and existing paintcanvas.apply_paint(point, self.color, self.width, self.wetness)
This example shows the control flow rather than a complete physical simulation. The path-generation, jitter, and paint-application functions represent the underlying rendering logic.
3. Feedback-Based Correction and Artistic Style
Errors are inevitable when a painting is constructed through many individual operations. An important feature of the code-driven approach is its ability to support a closed feedback loop.
In a workflow that treats deposited strokes as irreversible, the model must repeatedly inspect the current canvas and decide how to proceed. That inspection can rely on multimodal interpretation of rendered output or on execution feedback and logs.
If part of a painting looks wrong—for example, a bird’s back appears too narrow or its outlines become cluttered—the model can plan additional strokes to cover and correct the area rather than erase the existing work.
This cycle of execution, observation, and adjustment is characteristic of an agentic workflow. The next action depends on the state produced by previous actions, not just on the original request.
Developing a Consistent Visual Style
Code-driven painting also provides a way to guide stylistic development.
Rather than asking the model to closely imitate an existing artist, a developer can prompt it to create a fictional artist persona with an invented background and preferences for particular materials.
Once that persona is established, early paintings can serve as the basis for natural-language feedback. The model then adjusts brush parameters and painting-engine logic in response.
Across multiple iterations, this process can help establish a consistent, distinctive visual style. The persona provides creative direction, while the code provides concrete mechanisms for expressing it through brushwork, texture, and paint behavior.
4. Implications for Developers and Enterprise Workflows
The broader significance of this approach lies in using LLM-generated code to control a complex simulated process. Painting is one example of a workflow in which a high-level objective is translated into many coordinated, inspectable actions.
Decomposing and Executing Complex Tasks
Breaking a detailed painting into thousands or tens of thousands of brushstroke operations illustrates a general pattern: decompose a complex objective into smaller instructions, then execute them through software.
Related applications include:
- Parametric generation of CAD drawings.
- Layered rendering of complex data visualizations.
- Motion-path planning for robotic arms in industrial workflows.
In these settings, generated intermediary code can connect high-level intent with fine-grained execution controls. The painting example demonstrates the task-decomposition pattern rather than establishing production readiness for every adjacent application.
Building Cloud-Based Execution Pipelines
Rendering tasks with long runtimes and repeated calculations can be organized as cloud-native workflows.
The source scenario uses Baidu AI Cloud as an example deployment environment: generated painting-engine code runs in containers, while elastic compute resources support rendering. The surrounding pipeline uses:
- Message queues to pass execution status asynchronously between rendering rounds.
- Object storage to persist canvas snapshots at different stages.
- Logging services to monitor execution exceptions and error rates.
This architecture separates code execution, state persistence, and operational monitoring. A cloud-native deployment can support availability and observability as rendering jobs become more complex.
Improving Inspectability and Control
Compared with receiving only a finished image, a code-driven workflow exposes the instructions used to construct it.
Developers can review the generated Python code and adjust physical parameters, such as paint-drying speed or a modeled canvas-friction coefficient. These controls provide a structured framework for exploring visual outcomes.
This inspectability is particularly relevant to commercial design workflows that require consistent styling and careful copyright review. It makes the construction process easier to examine, although visibility into the code should not be confused with a guarantee of legal clearance.
Conclusion
Using an LLM to write a virtual painting program offers an alternative to generating images directly with a conventional image model. By constructing an image stroke by stroke, the system connects code generation with physical simulation, visual feedback, and iterative correction.
The approach highlights the potential of LLMs to turn high-level creative goals into detailed executable processes. Its relevance extends beyond painting to procedural generation, agentic automation, and the orchestration of complex computational tasks.
As code-generation capabilities improve and cloud infrastructure supports larger iterative workloads, code-driven simulation offers a promising direction for combining creative flexibility with inspectable, fine-grained control.
