Procedurally Generating Images in the Style of Piet Mondrian
Python
Latest commit 3991e63 Sep 24, 2013 @fogleman README updated
Permalink
Failed to load latest commit information.
README.md README updated Sep 24, 2013
main.py initial commit Sep 24, 2013

README.md

Procedurally Generating Images in the Style of Piet Mondrian

Introduction

Piet Mondrian was a Dutch painter. His paintings with orthogonal lines and rectangular splashes of primary colors on white backgrounds are very recognizable.

I wondered what it would take to programmatically generate Mondrian-esque images. Here's what I came up with.

The Algorithm

The algorithm operates on a two-dimensional grid. Initially, the grid is empty (white or 0) with the perimeter filled in (black or 1) as a sentinel border. The black areas indicate where the lines are to be drawn.

Next, the algorithm chooses a random number of times to "split" the grid into regions. I choose a random number between 4 and 16 (inclusive) for this.

For each "split" operation, the algorithm randomly chooses to split vertically or horizontally. Depending on the orientation chosen, a random X or a random Y value is chosen as the partition line.

The algorithm scans the partition line, looking for existing walls. (Shown in circles.) These existing walls are candidate endpoints for the new line segment. (Initially, only the perimeter border is present, so the first partition will span the entire width or height of the grid.) The algorithm randomly chooses two of the points found and draws a new line between those points.

This process repeats until the desired number of split operations have been completed.

Here, three endpoints are available. Two are chosen randomly for the next line.

The algorithm keeps track of prior split operations so that no two splits are too close together. (No splits are allowed in the gray region shown in the image. The amount of padding is configurable.)

After performing all of the splits, the grid looks something like this.

Next, the algorithm locates all of the distinct regions of the grid using flood fills. The algorithm chooses a random number of regions to "fill" with a color. For each fill, it chooses a random color to use - red, blue or yellow.

Finally, the perimeter border is chopped off and we have the final result.

And that's it - you can have all the procedurally-generated, Mondrian-esque images you want!

Samples