-
Notifications
You must be signed in to change notification settings - Fork 42
/
mandelbrot.py
54 lines (45 loc) · 1.4 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
54
import tensorflow as tf
import numpy as np
from PIL import Image
R = 4
ITER_NUM = 200
def color(z, i):
# From: https://www.reddit.com/r/math/comments/2abwyt/smooth_colour_mandelbrot/
if abs(z) < R:
return 0, 0, 0
v = np.log2(i + R - np.log2(np.log2(abs(z)))) / 5
if v < 1.0:
return v**4, v**2.5, v
else:
v = max(0, 2 - v)
return v, v**1.5, v**3
def gen_mandelbrot(Z):
xs = tf.constant(Z.astype(np.complex64))
zs = tf.Variable(xs)
ns = tf.Variable(tf.zeros_like(xs, tf.float32))
with tf.Session():
tf.global_variables_initializer().run()
zs_ = tf.where(tf.abs(zs) < R, zs**2 + xs, zs)
not_diverged = tf.abs(zs_) < R
step = tf.group(
zs.assign(zs_),
ns.assign_add(tf.cast(not_diverged, tf.float32))
)
for i in range(ITER_NUM):
step.run()
final_step = ns.eval()
final_z = zs_.eval()
r, g, b = np.frompyfunc(color, 2, 3)(final_z, final_step)
img_array = np.dstack((r, g, b))
return Image.fromarray(np.uint8(img_array * 255))
if __name__ == '__main__':
start_x = -2.5 # x range
end_x = 1
start_y = -1.2 # y range
end_y = 1.2
width = 1000 # image width
step = (end_x - start_x) / width
Y, X = np.mgrid[start_y:end_y:step, start_x:end_x:step]
Z = X + 1j * Y
img = gen_mandelbrot(Z)
img.save('mandelbrot.png')