Fractals with Python
It’s impressing how easy it is to draw fractals with Python. Using the ubercool Python Imaging Library (http://www.pythonware.com/products/pil/index.htm) and native complex number support in Python you can code an elaborate and easy to understand fractal generator in less than 50 lines of code:
#!/usr/bin/python import Image, ImageDraw, math, colorsys
dimensions = (800, 800) scale = 1.0/(dimensions[0]/3) center = (2.2, 1.5) # Use this for Mandelbrot set #center = (1.5, 1.5) # Use this for Julia set iterate_max = 100 colors_max = 50
img = Image.new(“RGB”, dimensions) d = ImageDraw.Draw(img)
Calculate a tolerable palette
palette = [0] * colors_max for i in xrange(colors_max): f = 1-abs((float(i)/colors_max-1)**15) r, g, b = colorsys.hsv_to_rgb(.66+f/3, 1-f/2, f) palette[i] = (int(r255), int(g255), int(b*255))
Calculate the mandelbrot sequence for the point c with start value z
def iterate_mandelbrot(c, z = 0): for n in xrange(iterate_max + 1): z = z*z +c if abs(z) > 2: return n return None
Draw our image
for y in xrange(dimensions[1]): for x in xrange(dimensions[0]): c = complex(x scale - center[0], y scale - center[1])
n = iterate_mandelbrot(c) # Use this for Mandelbrot set
#n = iterate_mandelbrot(complex(0.3, 0.6), c) # Use this for Julia set
if n is None:
v = 1
else:
v = n/100.0
d.point((x, y), fill = palette[int(v * (colors_max-1))])
del d img.save(“result.png”)
Some example pictures:
.
Write a comment