-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (107 loc) · 3.65 KB
/
main.py
File metadata and controls
133 lines (107 loc) · 3.65 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import numpy
from PIL import Image
from multiprocessing import Process, Queue
import mandelbrot
import colormaps as ColorMaps
import settings as Settings
num_processes = 16
processes = []
slices = Queue()
def main():
maps = ColorMaps.load()
settings = Settings.load()
x = settings['x']
y = settings['y']
zoom = settings['zoom']
width = settings['width']
height = settings['height']
iterations = settings['iterations']
fractal = settings['fractal']
map_name = settings['color']['map']
map_colortype = settings['color']['type']
filename = 'fractal.png'
# Get the color map object specified by the user.
c_map = maps.get_color_map(map_name)
# Compute necessary values
units_per_pixel = 5 / (zoom * width)
r_max = x + (width / 2) * units_per_pixel
c_max = y + (height / 2) * units_per_pixel
r_min = x - (width / 2) * units_per_pixel
c_min = y - (height / 2) * units_per_pixel
width_total = 0
for i in range(num_processes - 1):
current_width = width // num_processes
width_total = width_total + current_width
# Create the current slice.
p = Process(target=generate_slice,args=(
current_width,
height,
r_min + i * current_width * units_per_pixel,
c_min,
r_min + (i+1) * current_width * units_per_pixel,
c_max,
c_map,
map_colortype,
iterations,
i,
slices
)
)
processes.append(p)
p.start()
# Create the last slice. The last slice might have a different size to the others.
p = Process(target=generate_slice,args=(
width - width_total,
height,
r_min + width_total * units_per_pixel,
c_min,
r_max,
c_max,
c_map,
map_colortype,
iterations,
num_processes-1,
slices
)
)
processes.append(p)
p.start()
# Get all the slices from the child processes.
slice_map = {}
for i in range(num_processes):
slice = slices.get()
slice_map[slice[0]] = slice[1]
# Get a sorted list of keys from the slice map.
key_list = []
for key in slice_map.keys():
key_list.append(key)
key_list.sort()
# Create a list of ordered slices.
slice_list = []
for key in key_list:
slice_list.append(slice_map.get(key))
# Merge the slices
image = Image.new('RGB',(width,height))
width_total = 0
for slice in slice_list:
image.paste(slice,(width_total,0))
width_total = width_total + slice.size[0]
# Save the image
image.save(filename)
def generate_slice(width, height, r_min, c_min, r_max, c_max, color_map, color_type, iterations, slice_number, queue):
# Calculate the delta values
r_delta = (r_max - r_min) / width
c_delta = (c_max - c_min) / height
# Create an image to store the slice
img = numpy.zeros((height,width,3), numpy.uint8)
# Compute the slice
for i in range(width):
for j in range(height):
r = r_min + i * r_delta
c = c_max - j * c_delta
img[j,i] = color_map.get_color(mandelbrot.compute(r,c,iterations), color_type).to_rgb_channel_list()
# Store the 2D array in the image object and return it to the queue.
img = Image.fromarray(img,'RGB')
queue.put((slice_number,img))
if __name__ == '__main__':
main()