-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
create_fractal.py
57 lines (53 loc) · 1.72 KB
/
create_fractal.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
54
55
56
57
from __future__ import print_function, division
class Img(object):
def __init__(self, width, height):
self.width = width
self.height = height
self.data = bytearray(width*height)
def create_fractal(image, iters, func, oneval):
''' Call a function for each pixel in the image, where
-2 < real < 1 over the columns and
-1 < imag < 1 over the rows
'''
pixel_size_x = 3.0 / image.width
pixel_size_y = 2.0 / image.height
for y in range(image.height):
imag = y * pixel_size_y - 1
yy = y * image.width
for x in range(image.width):
real = x * pixel_size_x - 2
func(real, imag, iters, oneval)
image.data[yy + x] = oneval[0]
def mandel(x, y, max_iters, value):
"""
Given the real and imaginary parts of a complex number,
determine if it is a candidate for membership in the Mandelbrot
set given a fixed number of iterations.
"""
i = 0
c = complex(x,y)
z = 0.0j
for i in range(max_iters):
z = z*z + c
if (z.real*z.real + z.imag*z.imag) >= 4:
value[0] = i
return 0
value[0] = max_iters
return max_iters
if __name__ == '__main__':
from timeit import default_timer as timer
from PIL import Image
# Pure python
width = 1500
height = 1000
image = Img(width, height)
s = timer()
oneval = bytearray(1)
create_fractal(image, 20, mandel, oneval)
e = timer()
elapsed = e - s
import platform
imp = platform.python_implementation().lower()
print('pure {} required {:.2f} millisecs'.format(imp, 1000*elapsed))
im = Image.frombuffer("L", (width, height), image.data, "raw", "L", 0, 1)
im.save('{}.png'.format(imp))