-
Notifications
You must be signed in to change notification settings - Fork 1
/
generators.py
144 lines (112 loc) · 4.99 KB
/
generators.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
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
134
135
136
137
138
139
140
141
142
143
144
from PIL import Image
import math
def _limit(command, param):
if param == "fade":
return 100
elif command == "multijulia":
if param == "zoom":
return 100
elif param == "n":
return 10
else:
return 200
elif command in ["tricorn", "julia", "mandelbrot", "burning_ship"]:
if param != "zoom":
return 200
else:
return 100
elif command == "grayscale_noise":
return 127
def grayscale_noise(image, sigma=127, fade=100):
return Image.blend(image, Image.effect_noise(image.size, sigma).convert("RGB"), fade / 100)
def _run_mandelbrot(w, h, zoom, move_x, move_y):
mandelbrot_image = Image.new("RGB", (w, h), (255, 255, 255))
mandelbrot_pixels = mandelbrot_image.load()
for px in range(w):
for py in range(h):
x0 = 1.5 * (px - w / 2) / (0.5 * zoom * w) + move_x
y0 = 1 * (py - h / 2) / (0.5 * zoom * h) + move_y
x = 0
y = 0
i = 0
while x * x + y * y < 4 and i < 255:
tmp = x * x - y * y + x0
y = 2 * x * y + y0
x = tmp
i += 1
mandelbrot_pixels[px, py] = (i << 21) + (i << 10) + i * 8
return mandelbrot_image
def mandelbrot(image, zoom=100, move_x=100, move_y=100):
return _run_mandelbrot(image.width, image.height, zoom / 100, (move_x - 100) / 100, (move_y - 100) / 100)
def _run_tricorn(w, h, zoom, move_x, move_y):
tricorn_image = Image.new("RGB", (w, h), (255, 255, 255))
tricorn_pixels = tricorn_image.load()
for px in range(w):
for py in range(h):
x = 1.5 * (px - w / 2) / (0.5 * zoom * w) + move_x
y = 1 * (py - h / 2) / (0.5 * zoom * h) + move_y
zx = x
zy = y
i = 0
while zx * zx + zy * zy < 4 and i < 255:
xtemp = zx * zx - zy * zy + x
zy = -2 * zx * zy + y
zx = xtemp
i += 1
tricorn_pixels[px, py] = (i << 21) + (i << 10) + i * 8
return tricorn_image
def tricorn(image, zoom=100, move_x=100, move_y=100):
return _run_tricorn(image.width, image.height, zoom / 100, (move_x - 100) / 100, (move_y - 100) / 100)
def _run_burning_ship(w, h, zoom, move_x, move_y):
tricorn_image = Image.new("RGB", (w, h), (255, 255, 255))
tricorn_pixels = tricorn_image.load()
for px in range(w):
for py in range(h):
x = 1.5 * (px - w / 2) / (0.5 * zoom * w) + move_x
y = 1 * (py - h / 2) / (0.5 * zoom * h) + move_y
zx = x
zy = y
i = 0
while zx * zx + zy * zy < 4 and i < 255:
xtemp = zx * zx - zy * zy + x
zy = abs(2 * zx * zy) + y
zx = xtemp
i += 1
tricorn_pixels[px, py] = (i << 21) + (i << 10) + i * 8
return tricorn_image
def burning_ship(image, zoom=100, move_x=100, move_y=100):
return _run_burning_ship(image.width, image.height, zoom / 100, (move_x - 100) / 100, (move_y - 100) / 100)
def _run_julia(w, h, cx, cy, move_x, move_y, zoom):
julia_image = Image.new("RGB", (w, h), (255, 255, 255))
julia_pixels = julia_image.load()
for x in range(w):
for y in range(h):
zx = 1.5 * (x - w / 2) / (0.5 * zoom * w) + move_x
zy = 1 * (y - h / 2) / (0.5 * zoom * h) + move_y
i = 0
while zx * zx + zy * zy < 4 and i < 255:
tmp = zx * zx - zy * zy + cx
zy = 2 * zx * zy + cy
zx = tmp
i += 1
julia_pixels[x, y] = (i << 21) + (i << 10) + i * 8
return julia_image
def julia(image, cx=179, cy=37, zoom=100, move_x=100, move_y=100):
return _run_julia(image.width, image.height, (cx - 100) / 100, (cy - 100) / 100, (move_x - 100) / 100, (move_y - 100) / 100, zoom / 100)
def _run_multijulia(w, h, cx, cy, n, move_x, move_y, zoom):
julia_image = Image.new("RGB", (w, h), (255, 255, 255))
julia_pixels = julia_image.load()
for x in range(w):
for y in range(h):
zx = 1.5 * (x - w / 2) / (0.5 * zoom * w) + move_x
zy = 1 * (y - h / 2) / (0.5 * zoom * h) + move_y
i = 0
while zx * zx + zy * zy < 4 and i < 255:
tmp = pow(zx * zx - zy * zy + cx, n / 2) * math.cos(n * math.atan2(zy, zx)) + cx
zy = pow(zx * zx - zy * zy + cx, n / 2) * math.sin(n * math.atan2(zy, zx)) + cy
zx = tmp
i += 1
julia_pixels[x, y] = (i << 21) + (i << 10) + i * 8
return julia_image
def multijulia(image, cx=179, cy=37, n=4, move_x=100, move_y=100, zoom=100):
return _run_multijulia(image.width, image.height, (cx - 100) / 100, (cy - 100) / 100, n, (move_x - 100) / 100, (move_y - 100) / 100, zoom / 100)