Generate perfect (only a single solution without retracing steps) mazes using recursive backtracing. All cells of the maze will be connected with each other, meaning there are no unreachable places.
Allows creating masked mazes by specifying an image. A mask image limits the possible space for maze generation and allows to create mazes of arbitrary shapes.
Also contains a script that creates mask images from text.
- Python >= 3.6
- numpy
- PIL
These options can be specified for normal and masked maze generation. To select
between the two, specify generate
or mask
after the general options. Both commands
have specific suboptions, see Maze generation options
and Mask image options.
$ python maze.py
usage: maze.py [-h] [-f FILENAME] [-s SEED] [-o ORIGIN ORIGIN] [-c CELLSIZE]
[-l LINEWIDTH]
{generate,mask} ...
Generate mazes.
positional arguments:
{generate,mask} Select between just maze generation with width/height
or generating a maze with a mask.
generate Generate a maze within a rectangle.
mask Apply mask to limit maze.
optional arguments:
-h, --help show this help message and exit
-f FILENAME, --filename FILENAME
Filename that is used to save the maze.
-s SEED, --seed SEED Seed for random generator.
-o ORIGIN ORIGIN, --origin ORIGIN ORIGIN
x y coordinate of the start cell in the maze
-c CELLSIZE, --cellsize CELLSIZE
Cell size in pixels for plotting.
-l LINEWIDTH, --linewidth LINEWIDTH
Line width of cell walls for plotting in pixels.
Specify dimensions of maze
python maze.py generate -h
usage: maze.py generate [-h] width height
positional arguments:
width Width of the maze in cells.
height Height of the maze in cells.
optional arguments:
-h, --help show this help message and exit
Example:
$ python maze.py -f example.png -s mazemaker generate 100 50
Will generate a maze with the seed mazemaker, width of 100 and height of 50 cells and save it in a file named example.png.
Specify which mask image to use. One pixel of the mask image will be one cell in the final maze. Mask images are expected to be saved with 8-bit pixels, black and white. A black pixel blocks traversal of the algorithm, allowing to build walls.
python maze.py mask -h
usage: maze.py mask [-h] [-m MASKIMG]
optional arguments:
-h, --help show this help message and exit
-m MASKIMG, --maskimg MASKIMG
Image to use as mask, where only white pixels can be
visited by the algorithm, while black pixels are
forbidden. If this is specified, the maze will be of
the same dimensions as the mask image.
Example:
python maze.py -f images/dogmaze.png -o 26 30 -l 2 mask -m images/dog_outline.png
Generate a maze from a mask image (dog_outline.png). Starting cell is pixel x:26, y:30 in the mask image. Wall thickness is two pixels. Save it in dogmaze.png
If you want to generate the maze inside the letters the font has to connect letters together. I found the Unicorn font from Nick Curtis which is free and can be downloaded from DaFont.
python create_mask_image.py -h
usage: create_mask_image.py [-h] [-f FILENAME] [-s FONTSIZE] [-b BORDERSIZE]
[-i]
text
Generate a mask image to be used with the maze generator. This script creates
a mask image from the text string.Areas in black will be treated as walls by
the algorithm, acting as aborder of the maze generation.
positional arguments:
text Text to be used as mask. Will be converted to lower
case
optional arguments:
-h, --help show this help message and exit
-f FILENAME, --filename FILENAME
Filename under which the mask image will be saved.
-s FONTSIZE, --fontsize FONTSIZE
Font size in points to use for text. A size <16 will
be too small for letters to connect with the default
Unicorn font.
-b BORDERSIZE, --bordersize BORDERSIZE
Thickness of space around the text bounding box to the
image border in pixels.
-i, --invert Flag argument. If not specified (default) the letters
will be white with a black border. This means the maze
can be generated within the letters. Otherwise, if
this option is specified, the letters will be the
completely black and the maze will be generated around
them.
Example:
$ python create_mask_image.py mazemaker -b 8 -s 32
Creates a mask image from the text mazemaker with font size 32 and border size 8. Saves in default mask.png in the current directory.