Mandelbrot Fractal Render Set in Python

Simply put, the fractal — it is an endless pattern. Fractals — they are infinitely complex patterns that are self-similar at different scales. They are created by repeating a simple process in a continuous feedback loop. Recursion-driven, fractals — these are images of dynamic systems — images of Chaos. 
Geometrically, they exist between our usual dimensions. Fractal patterns are extremely familiar as nature is full of fractals. For example: trees, rivers, coastlines, mountains, clouds, seashells, hurricanes, etc. Abstract fractals — such as the Mandelbrot set — can be generated by a computer calculating a simple equation over and over.

Mandelbrot set:
Mandelbrot set — it is a set of complex numbers c, for which the function does not diverge from z when repeated = 0, that is, for which the sequence and so on remains limited in absolute value. Simply put, the Mandelbrot set — it is a special set of complex numbers that has a very sinuous fractal border when plotting.

Installing the required Python modules:

 pip install pillow pip install numpy 

Code # 1:

# Python code for the Mandelbrot fractal

  
# Import required libraries

from PIL import Image

from numpy import complex , array

import colorsys

 
# mustache Setting the width of the output image to 1024

WIDTH = 1024

 
# function to return a tuple of colors
# as an integer rgb value

def rgb_conv (i ):

color = 255 * array (colorsys.hsv_to_rgb (i / 255.0 , 1.0 , 0.5 ))

return tuple (color.astype ( int ))

 
# function defining a Mandelbrot

def mandelbrot (x, y):

c0 = complex (x, y)

c = 0

for i in range ( 1 , 1000 ):

if abs (c) & gt;  2 :

return rgb_conv (i)

c = c * c + c0

return ( 0 , 0 , 0 )

 
# create a new image in RGB mode

img = Image. new ( `RGB` , (WIDTH, int (WIDTH / 2 )))

pixels = img.load ()

 

for x in range (img.size [ 0 ]):

 

# display progress in percent

print ( "%. 2f %%" % (x / WIDTH * 100.0 )) 

for y in range (img.size [ 1 ]):

pixels [x, y] = mandelbrot ((x - ( 0.75 * WIDTH)) / (WIDTH / 4 ),

  (y - (WIDTH / < / code> 4 )) / (WIDTH / 4 ))

 
# display the created fractal after
# completion of the specified number of iterations
img.show ()

Output:

Code # 2:

# Mandelbrot fractal
# FB - 201003254

from PIL import Image

  
# drawing area

xa = - 2.0

xb = 1.0

ya = - 1.5

yb = 1.5

 
Maximum number of iterations

maxIt = 255  

 
# Image size

imgx = 512

imgy = 512

image = Image.new ( " RGB " , (imgx, imgy))

 

for y in range (imgy):

zy = y * (yb - ya) / (imgy - 1 + ya

  fo r x in range (imgx):

  zx = x * (xb - xa) / (imgx ​​ - 1 + xa

z = zx + zy * 1j

c = z < / code>

for i in range (maxIt):

if abs (z) & gt;  2.0 : break

z = z * z + c

image.putpixel ((x, y), (i % 4 * 64 , i % 8 * 32 , i % 16 * 16 ))

 
image.show ()

Output: