I created a program in python that generates an image of the mandelbrot set. The only problem I have is that the program is quite slow, it takes about a quarter of an hour to generate the following image of 2000 by 3000 pixels:
I first created a matrix of complex numbers using numpy according to amount of pixels. I also created an array for the image generation.
import numpy as np
from PIL import Image
z = 0
real_axis = np.linspace(-2,1,num=3000)
imaginary_axis = np.linspace(1,-1,num=2000)
complex_grid = [[complex(np.float64(a),np.float64(b)) for a in real_axis] for b in imaginary_axis]
pixel_grid = np.zeros((2000,3000,3),dtype=np.uint8)
Then I check whether each complex number is in the mandelbrot set or not and give it an RGB colour code accordingly.
for complex_list in complex_grid:
for complex_number in complex_list:
for iteration in range(255):
z = z**2 + complex_number
if (z.real**2+z.imag**2)**0.5 > 2:
pixel_grid[complex_grid.index(complex_list),complex_list.index(complex_number)]=[iteration,iteration,iteration]
break
else:
continue
z = 0
Finally I generate the image using the PIL library.
mandelbrot = Image.fromarray(pixel_grid)
mandelbrot.save("mandelbrot.png")
I am using jupyter notebook and python 3. Hopefully some of you can help me improve the performance of this program or other aspects of it.
