Send Close Add comments: (status displays here)
Got it!  This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Python: Maze generation using PIL
by RS  admin@robinsnyder.com : 1024 x 640


1. Python: Maze generation using PIL
This page uses Python to do recursive maze generation using the PIL (Python Imaging Library).

2. Recursive maze generation and visualization
Here are links to a series on recursive maze generation and visualization (more coming) It is sometimes useful to create custom images in Python. The ImageDraw class in the PIL can be useful for this.

One can create new images or read and annotate existing images.

It as always useful to create a custom class. But, for example purposes, no classes are used.

3. Colors
The ImageDraw class uses colors are represented as a tuple of the integer values for red, green, and blue. I find it easier to work with the hex forms of red, green and blue. The color method converts from hex to a tuple representation.


4. Color examples
A Lua command line console program is now presented to create a maze as an image.

This may not be the best Lua program for this task, but is sufficient for the present purposes.

5. Algorithm pesudo-code
Here is a pseudo-code for the maze generation algorithm as described to make a maze of rows and columns.
Main: Start with an empty canvas. Outline the maze limits with an entry and exit point on opposite sides. Call MazeGenerate with the rows and columns to be filled in. MazeGenerate: If the rows and columns requested are both greater than 1 Then If the height is greater than the width Then Divide the grid into top and bottom parts. Draw a (horizontal) line between the parts, leaving a gap to pass through. Call MazeGenerate on the top part Call MazeGenerate on the bottom part Else Divide the grid into left and right parts. Draw a (vertical) line between the parts, leaving a gap to pass through. Call MazeGenerate on the left part Call MazeGenerate on the right part End If End If

The actual code fills in details that are not needed at the higher level of the algorithm description. For example, the side and gap lengths, and exact x and y coordinates are omitted. As another example, the division into parts can be done using pseudo-random numbers, but that is omitted from the algorithm.

For more information, see the following: Maze algorithm explanation using GIF images Here is the Python code [#2]

Here is the output of the Python code.

Here is the image created by the Python code. Maze

by RS  admin@robinsnyder.com : 1024 x 640