This is an implementation of a masked Maze Generator using the method of depth-first search with recursive backtracking. It generates the maze inside a mask.
This method of generating mazes is described in the following Wikipedia page:
Maze generation algorithm
MIT open source
This program is a reimplementation in Python of the program written in Javascript for processing normal mazes, but in this reimplementation I modified it heavily, I changed the render, I added a user defined mask, SVG output, PNG output, animated GIF output and MPEG4 generation.
See also the following great videos explaining beautifully the Javascript code and how the algorithm of backtracking works.
Javscript github for code train
Videos:
Vídeo part 1
Video part 2
Video part 3
Video part 4
Features:
- It generates a maze for a given PNG mask input image with any color or n colors corresponding to the mask, parameterized. With any resolution.
- The length of the squares is configurable.
- The colors are configurable (Background color, lines colors, visited cell’s color, current cell color)
- The line with is configurable.
- It generates at the beginning a PNG sample with the position of the center of each square over the mask, as a black image with dotted white point’s.
- It generates images for each frame in SVG and PNG format.
- It generates an animated GIF file.
- It generates a highly compressed MP4 file (small file size) using with FFMPEG.
- It is reasonably fast, even on older hardware.
I used the Anaconda Python 3.7 installation with a virtual environment.
The required libs are:
- svgwrite
- svglib
- imageio
- pillow
- moviepy [Not using this one, for MP4 encoding use FFMPEG program ]
- First you have to generate the PNG mask in black with a background in other color (like white), or use a image and obtain with a image editor the color values that you will parameterize inside the source code on the test_01 function (see the commented examples). It can also also be a logo or a photo. This mask can be inside the directory /png_masks/, although you can put it in a different path. In this phase, if you have several blob's or letters that you would like to have filled with maze cells please connect them in the mask with small rectangles with at least the cell with length.
- Configure the parameters inside the test_01 function,a t the end of the file, see commented examples.
- Then you will run the code with the command "python masked_maze_generator_core.py" and check inside the directories what is being generated. The order is:
- n frames in SVG
- n frames in PNG
- One animated GIF
- Then generate manually the MPEG4 file (MP4) with FFMPEG
- The command to generate the MPEG 4 file with FFMPEG is: ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.mp4 , in with the animated.gif and video.mp4 as to be changed to your specific case. '''
Note: This program was tested on Windows and Linux but it should also work on MAC, but i didn't tested.
Have fun!
## How to setup the virtual environment and the packages in venv for the project Linux and Windows.
See the conda version:
$ conda -V
Update conda
$ conda update conda
Create a virtual environment for your project (substitute yourenvname by the name of the environment ex: maze_generator and the x.x by 3.7 the version of Python in the future environment).
$ conda create -n yourenvname python=x.x anaconda
ex: $ conda create -n maze_generator python=3.7 anaconda
See the environments that were created.
$ conda info -e
To activate the environment
$ source activate yourenvname
ex: $ source activate maze_generator
or
$ conda activate maze_generator
To desactivate an active environment
$ source activate yourenvname
ex: $ source desactivate
or
$ conda deactivate
Install additional Python packages to a virtual environment.
$ conda install -n yourenvname [package]
ex: $ conda install -n maze_generator svgwrite
Delete a no longer needed virtual environment
$ conda remove -n yourenvname -all
ex: $ conda remove -n maze_generator -all
### How to instal the libs/packages for the maze_generator package. pip install svgwrite
pip install svglib==0.9.0
pip install imageio
pip install pillow
pip install cssselect2
Install FFMPEG:
Ubuntu already comes with FFMPEG but you may need to update it.
$ sudo apt update
$ sudo apt install ffmpeg
$ ffmpeg -version
Just for info:
$ ffmpeg -encoders
$ ffmpeg -decoders
Note: If you are using Linux delete the ".env" file and rename the ".env_linux" file to ".env". Adjust any part of the path if needed. This file is necessary in Visual Studio Code with virtual environments in Anaconda because it doesn't make automatically the activation of the virtual environment.