-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·71 lines (59 loc) · 1.92 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
from mandelbrot import mandelbrot # Max iter within bounds check
from PIL import Image, ImageDraw
from PARAMS import (MAX_ITER,
IMG_WIDTH, IMG_HEIGHT,
REAL_START, REAL_END,
IM_START, IM_END,
BW, STORE, IMG_PROGRAM) # Params
from multiprocessing import Pool
# Creates new image and drawing
img = Image.new("RGB", (IMG_WIDTH, IMG_HEIGHT), (255, 255, 255))
drawing = ImageDraw.Draw(img)
# Creates palette as needed
palette = []
if not BW:
for rg in range(0, 256, 5):
palette.append((255 - rg, rg, 0))
for gb in range(0, 256, 5):
palette.append((0, 255 - gb, gb))
# Create pixel grid
pixels = []
for x in range(0, IMG_WIDTH):
for y in range(0, IMG_HEIGHT):
pixels.append([x, y])
def colorPixel(pixel: [int, int]):
"""
Color each pixel acording to its max element within bounds on the set
"""
# Generates according complex number
c = complex(REAL_START + (pixel[0] / IMG_WIDTH) * (REAL_END - REAL_START),
IM_START + (pixel[1] / IMG_HEIGHT) * (IM_END - IM_START))
# Checks max bounded iteration
i = mandelbrot(c)
# Apply color
if BW:
tone = int(i/MAX_ITER * 255)
color = (tone, tone, tone)
else:
color = palette[int(i/MAX_ITER * (len(palette) - 1))]
return color
def main():
# Creates pixel color array
colors = []
with Pool() as p:
# Maps pixels to pixel coloring function asynchronously
colors = p.map(colorPixel, pixels)
# Awaits processes and waits for them to finish
p.close()
p.join()
# Draws each pixel with given color
for pixel in range(0, len(pixels)):
drawing.point(pixels[pixel], colors[pixel])
# Shows generated image
if STORE:
img.save("set.png", "PNG")
else:
img.show(command=IMG_PROGRAM)
if __name__ == "__main__":
main()