-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
56 lines (45 loc) · 1.41 KB
/
plot.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
from PIL import Image, ImageDraw
from mandelbrot import mandelbrot, MAX_ITER
from collections import defaultdict
from math import floor, ceil
def linear_interpolation(color1, color2, t):
return color1 * (1 - t) + color2 * t
# Image size (pixels)
WIDTH = 600
HEIGHT = 400
# Plot window
RE_START = -2
RE_END = 1
IM_START = -1
IM_END = 1
histogram = defaultdict(lambda: 0)
values = {}
for x in range(0, WIDTH):
for y in range(0, HEIGHT):
# Convert pixel coordinate to complex number
c = complex(RE_START + (x / WIDTH) * (RE_END - RE_START),
IM_START + (y / HEIGHT) * (IM_END - IM_START))
# Compute the number of iterations
m = mandelbrot(c)
values[(x, y)] = m
if m < MAX_ITER:
histogram[floor(m)] += 1
total = sum(histogram.values())
hues = []
h = 0
for i in range(MAX_ITER):
h += histogram[i] / total
hues.append(h)
hues.append(h)
im = Image.new('HSV', (WIDTH, HEIGHT), (0, 0, 0))
draw = ImageDraw.Draw(im)
for x in range(0, WIDTH):
for y in range(0, HEIGHT):
m = values[(x, y)]
# The color depends on the number of iterations
hue = 255 - int(255 * linear_interpolation(hues[floor(m)], hues[ceil(m)], m % 1))
saturation = 255
value = 255 if m < MAX_ITER else 0
# Plot the point
draw.point([x, y], (hue, saturation, value))
im.convert('RGB').save('output.png', 'PNG')