-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathmandelbrot.py
54 lines (37 loc) · 1.73 KB
/
mandelbrot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Code Listing #18
"""
Mandelbrot fractal set generator
"""
# mandelbrot.py
import sys
import argparse
from PIL import Image
def mandelbrot_calc_row(y, w, h, image, max_iteration = 1000):
""" Calculate one row of the mandelbrot set with size wxh """
y0 = y * (2/float(h)) - 1 # rescale to -1 to 1
for x in range(w):
x0 = x * (3.5/float(w)) - 2.5 # rescale to -2.5 to 1
i, z = 0, 0 + 0j
c = complex(x0, y0)
while abs(z) < 2 and i < max_iteration:
z = z**2 + c
i += 1
# Color scheme is that of Julia sets
color = (i % 8 * 32, i % 16 * 16, i % 32 * 8)
image.putpixel((x, y), color)
def mandelbrot_calc_set(w, h, max_iteration=10000, output='mandelbrot.png'):
""" Calculate a mandelbrot set given the width, height and
maximum number of iterations """
image = Image.new("RGB", (w, h))
for y in range(h):
mandelbrot_calc_row(y, w, h, image, max_iteration)
image.save(output, "PNG")
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog='mandelbrot', description='Mandelbrot fractal generator')
parser.add_argument('-W','--width',help='Width of the image',type=int, default=640)
parser.add_argument('-H','--height',help='Height of the image',type=int, default=480)
parser.add_argument('-n','--niter',help='Number of iterations',type=int, default=1000)
parser.add_argument('-o','--output',help='Name of output image file',default='mandelbrot.png')
args = parser.parse_args()
print('Creating mandelbrot set with size %(width)sx%(height)s, #iterations=%(niter)s' % args.__dict__)
mandelbrot_calc_set(args.width, args.height, max_iteration=args.niter, output=args.output)