-
Notifications
You must be signed in to change notification settings - Fork 2
/
mandelbrot.py
56 lines (42 loc) · 1.35 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
55
56
# -*- coding: utf-8 -*-
from __future__ import print_function
# noinspection PyPackageRequirements
from PIL import Image, ImageDraw
from datetime import datetime
# size of the image
WIDTH = 2000
HEIGHT = 1600
# max iteration/points
MAX_ITER = 500
# plan drawn. reduce values to zoom on mandelbrot set
x_plot = (-3, 1)
y_plot = (-1.5, 1.5)
# init canvas
img = Image.new("RGB", (WIDTH, HEIGHT), "#010204")
draw = ImageDraw.Draw(img)
ratioW = (x_plot[1] - x_plot[0]) / float(WIDTH)
ratioH = (y_plot[1] - y_plot[0]) / float(HEIGHT)
# iteration through each pixels
for j in xrange(0, HEIGHT):
for i in xrange(0, WIDTH):
x0, y0 = ratioW * i + x_plot[0], ratioH * j + y_plot[0]
x = y = 0
iteration = 0
while x ** 2 + y ** 2 < 4 and iteration < MAX_ITER:
x_temp = x ** 2 - y ** 2 + x0
y = 2 * x * y + y0
x = x_temp
iteration += 1
# choose a color and draw point
if iteration == MAX_ITER:
color = 0
else:
color = iteration % MAX_ITER
draw.point((i, j), fill=(color, 0, 0))
# display percentage of pixels already calculated
if j % (HEIGHT / 100) == 0:
print('%d%%' % (j / (HEIGHT / 100)))
# save the fractal
now = datetime.now()
filename = now.strftime("%Y-%m-%d-%H:%M:%S") + '_mandelbrot.png'
img.save(filename, "PNG")