Visualize the Mandelbrot set in gigapixels (python)
The Mandelbrot set, named after mathematician Benoît Mandelbrot, is an example of a fractal in mathematics. The Mandelbrot set is important in chaos theory and it was one of the first examples of a simple formula creating complexity. Benoît Mandelbrot first observed the set when he used IBM’s newly developed computers to visualise mathematical formulas in the 70s. There is a great writeup on the history for fractals here: https://www.ibm.com/ibm/history/ibm100/us/en/icons/fractal/
Calculating and visualising the Mandelbrot set takes a significant bit of computing power, in this article I will present some code that can take full advantage of your computer’s resources. The output size you can achieve is only limited by your hardware.
There are countless implementations of the algorithm, in various programming languages, to be found on the internet. Instead of reinventing the wheel I will use the implementation used here and build on top of it: https://codereview.stackexchange.com/questions/216235/increase-performance-creating-mandelbrot-set-in-python/216240
The code is simple enough however it doesn’t take advantage of multithreading, thus it takes a long time to generate images of a large size. I’ve made some simple changes to the code to compute each column of pixels in a separate process.
The resulting script takes in 3 arguments
- Height of the image in pixels (width is 1.5x height)
- Number of processes (keep this < number of threads your computer has)
- File name of output (without the file extension)
The script uses f strings so you will also need python > 3.6 (or edit the script)
The command should be run as follows:
> python mandelbrot.py [Image Height] [# Processes] [Image Name]
The script is available here:https://gist.github.com/thezaza101/6435814500c170495f9b151d83bdccd5
I was able to create a 2.4 gigapixel (2,400,000,000) image on my PC with 64 GBs of ram . I ran the command:
> python mandelbrot.py 40000 30 image
https://drive.google.com/file/d/1ENDAYSgyRU04LjhQjpGwPxjHO4WpoU8y/view?usp=sharing
If you have more system resources at your disposal I would love to see the results!