-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.py
392 lines (259 loc) · 14.1 KB
/
runner.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import numpy as np
import time
import cv2
from ctypes import cdll, c_double, POINTER, c_int, c_uint32, c_uint16, c_uint8, c_bool, c_float #, c_longdouble
# Carregue a biblioteca
lib = cdll.LoadLibrary('./libfract.so')
fractal = lib.fractal
juliaset = lib.juliaset
lyapunov = lib.lyapunov
sandpile = lib.sandpile
lib.process_array.argtypes = [POINTER(c_uint32), POINTER(c_uint8), c_uint16, c_uint16, c_double, c_uint16, c_double]
lib.process_array.restype = None
lib.scale.argtypes = [POINTER(c_float), POINTER(c_float), c_int, c_float, c_float]
lib.scale.restype = None
fractal.argtypes = [POINTER(c_uint16), c_uint16, c_uint16, c_uint16, c_double, c_double, c_double, c_double, c_bool]
juliaset.argtypes = [POINTER(c_uint16), c_uint16, c_uint16, c_uint16, c_double, c_double, c_double, c_double, c_double, c_double, c_bool]
lyapunov.argtypes = [POINTER(c_uint16), c_uint16, c_uint16, c_uint16, c_double, c_double, c_double, c_double]
sandpile.argtypes = [POINTER(c_uint8), c_uint16, c_uint16, c_uint32, c_uint16]
def scale(input_array, min, max):
shape = input_array.shape
size = (input_array.size)
input_array = input_array.copy().reshape(-1).astype(np.float32)
input_array = input_array.ctypes.data_as(POINTER(c_float))
output_array = (c_float * (size))()
# Call the function
lib.scale(input_array, output_array, size, min, max)
# Convert the output array to a numpy array
output_array = np.ctypeslib.as_array(output_array).reshape(shape)
return output_array
def scale_fast(input, max):
return (input%(max+1))
# This can only scale positive numbers not negative numbers
def process_image(input_array, max_val, imgname):
width, height = input_array.shape
max = np.float64(np.max(input_array))
max_val = np.float64(max_val)
input_array = input_array.copy().reshape(-1).astype(np.uint32)
input_array = input_array.ctypes.data_as(POINTER(c_uint32))
output_array = (c_uint8 * (width * height* 3))()
# Call the function
lib.process_array(input_array, output_array, width, height, max_val, 5000, max)
del input_array
# Convert the output array to a numpy array
output_array = np.ctypeslib.as_array(output_array).reshape(width, height, 3 )
output_array = cv2.cvtColor(output_array, cv2.COLOR_BGR2RGB)
if "sandpile" in imgname:
output_array = cv2.resize(output_array, (width*4, height*4), interpolation=cv2.INTER_NEAREST)
cv2.imwrite(f'{imgname}.png', output_array)
print(f'{imgname}.png' )
def image_to_array(image_path, min=0, max=2**24-1):
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
array_image = np.array(img).astype(np.uint32)
if array_image.ndim != 3:
array_image = np.array(img.convert("RGBA"))
if array_image.ndim == 3:
if array_image.shape[2] == 4:
array_image = array_image[:, :, :-1]
if array_image.shape[2] == 3:
array_image = (array_image[:, :, 0]*(256**2)+array_image[:, :, 1]*(256)+array_image[:, :, 2])
return array_image
def palette_load(palette, top_colors=4, lake_palette=False, lake=False):
if (lake == False) or (lake_palette == False):
palette = image_to_array(palette)
unique_colors, counts = np.unique(palette, return_counts=True)
del palette
sorted_indices = np.argsort(counts)[::-1]
array_top_colors = unique_colors[sorted_indices][:top_colors]
return array_top_colors, False
else:
palette = image_to_array(palette)
unique_colors, counts = np.unique(palette, return_counts=True)
del palette
sorted_indices = np.argsort(counts)[::-1]
array_top_colors = unique_colors[sorted_indices][:top_colors]
# Lake palette
lake_palette = image_to_array(lake_palette)
unique_colors, counts = np.unique(lake_palette, return_counts=True)
del lake_palette
sorted_indices = np.argsort(counts)[::-1]
array_top_colors_lake = unique_colors[sorted_indices][:top_colors]
return array_top_colors, array_top_colors_lake
# Image with palette
def create_image(palette, data, filename, iterations, array_top_colors, lake=False, shift_palette=(0, 0) ):
data = data.copy().astype(np.uint32)
shape = data.shape
shape = (shape[1], shape[0])
data = data.reshape(shape)
array_top_colors = (
np.roll(array_top_colors[0], shift_palette[0]),
np.roll(array_top_colors[1], shift_palette[1]) if array_top_colors[1] is not False else False
)
if (lake and isinstance(array_top_colors[1], np.ndarray) and not ('lyapunov' in filename or 'sandpile' in filename)):
temp = data > iterations
data[temp] = scale_fast(data[temp], array_top_colors[1].shape[0] - 1) + iterations + 1
lake_indices = data - iterations - 1
data[temp] = np.take(array_top_colors[1], lake_indices[temp]) + iterations
data[~temp] = scale_fast(data[~temp], array_top_colors[0].shape[0] - 1)
data[~temp] = np.take(array_top_colors[0], data[~temp])
data[temp] = data[temp] - iterations
del temp
else:
data = scale_fast(data, array_top_colors[0].shape[0] - 1)
data = np.take(array_top_colors[0], data)
process_image(data.reshape(shape), np.max(data), filename)
# This helps you to aim by dividing in squares(grid)
def divide_in_squares(list_c, xmin, xmax, ymin, ymax):
list = list_c.copy()
list[:,:2] = list[:,:2]-1
for col, line, n_squares in list:
size_x = (xmax - xmin) / n_squares
size_y = (ymax - ymin) / n_squares
new_xmin = xmin + col * size_x
new_xmax = xmin + (col + 1) * size_x
new_ymin = ymin + line * size_y
new_ymax = ymin + (line + 1) * size_y
xmin, xmax, ymin, ymax = new_xmin, new_xmax, new_ymin, new_ymax
return xmin, xmax, ymin, ymax
width = int(1600) # I'm using ratio 1/1
height = int(1600) #2304
# Number of iterations
max_iter = 1000
# Sandpile max grains
max_grains = 3
# You can generate different types of fractals
fractals = {
'mandelbrot': True,
'juliaset': True,
'lyapunov': False, # Lyapunov seems to run very slowly at high resolution try it with 1600x1600.
'sandpile': False, # Try sandpile with less resolution and much more iterations(=grains of sand) to get better results, but don't let the colored area touch the border or you will get broken results.
}
zoom = False
max_zoom = 20 # How many images # it's gonna generate +n_coordinates more images than expected
per_zoom = 0.9 # Zooming after aiming: Using a value greater than 1.0 will zoom out; using a value less than 1.0 will zoom in
video_out = False # If you want to generate a video with the images
palette = "palette.png" # Palette location
use_palette = True
# How many top colors to use from the palette.png
top_colors = 24
shift_palette = (0, 0) # This shift the palette, you can set negative and positive integers.
# Julia set parameters
juliaset_c_real = -0.8
juliaset_c_imag = 0.16
# Makes the part that converges visible
lake = True
# Palette path to another palette image
lake_palette = "lake_palette.png"
# Here it's loading the palette before the generation and conversion
array_top_colors = palette_load(palette, top_colors, lake_palette, lake)
# Here you can move around
xmin_xmax = np.array([(-(16/6)), ((16/6))], dtype=np.float64) #-16/5, 16/5
ymin_ymax = np.array([-(16/6), (16/6)], dtype=np.float64) #-9/5, 9/5
# This part is to help you aim
n_coordinates = 5 # Number of coordinates to use, set False to not use it
# ([(column, line, grid nxn)])
coordinates = np.array([(1,2,3),(3,2,3),(1,2,3),(1,2,3),(3,3,5),(2,2,3),(1,2,3),(2,2,3),(1,2,3),(2,2,3)])
#coordinates = np.array([(1,1,3),(2,3,4),(1,2,3),(1,2,3),(3,3,5),(2,2,3),(1,2,3),(2,2,3),(1,2,3),(2,2,3)])
#coordinates = np.array([(3,3,3),(3,4,5),(1,2,3),(1,2,3),(3,3,5),(2,2,3),(1,2,3),(2,2,3),(1,2,3),(2,2,3)])
xmin, xmax, ymin, ymax = xmin_xmax[0], xmin_xmax[1], ymin_ymax[0], ymin_ymax[1]
# Uncomment the code below if you want to start at certain location
#xmin, xmax, ymin, ymax = divide_in_squares(coordinates[:(n_coordinates+1), :], xmin, xmax, ymin, ymax)
print("Your coordinates: ", xmin, xmax, ymin, ymax, "\n")
def generate(zoom, n_iter, max_zoom, max_iter, xmin, xmax, ymin, ymax):
prefix = ""
if zoom:
max_zoom = str(max_zoom)
target_length = len(max_zoom)+1
n_iter = str(n_iter)
n_iter = n_iter.zfill(target_length)
prefix = n_iter+"-"
for key, value in fractals.items():
# Mandelbrot Set
if (key == "mandelbrot") and (value):
gen_array = np.empty((height, width), dtype=np.uint16)
start_time = time.perf_counter()
fractal(gen_array.ctypes.data_as(POINTER(c_uint16)), width, height, max_iter, xmin, xmax, ymin, ymax, lake)
end_time = time.perf_counter()
print("Took ", end_time - start_time, "seconds to generate")
# Julia Set
if (key == "juliaset") and (value):
gen_array = np.empty((height, width), dtype=np.uint16)
start_time = time.perf_counter()
juliaset(gen_array.ctypes.data_as(POINTER(c_uint16)), width, height, max_iter, juliaset_c_real, juliaset_c_imag, xmin, xmax, ymin, ymax, lake)
end_time = time.perf_counter()
print("Took ", end_time - start_time, "seconds to generate")
# Lyapunov Set
if (key == "lyapunov") and (value):
gen_array = np.empty((height, width), dtype=np.uint16)
start_time = time.perf_counter()
lyapunov(gen_array.ctypes.data_as(POINTER(c_uint16)), width, height, max_iter, xmin, xmax, ymin, ymax)
end_time = time.perf_counter()
print("Took ", end_time - start_time, "seconds to generate")
# Abelian Sandpile Fractal
if (key == "sandpile") and (value):
gen_array = np.empty((height, width), dtype=np.uint8)
start_time = time.perf_counter()
sandpile(gen_array.ctypes.data_as(POINTER(c_uint8)), width, height, max_iter, max_grains)
end_time = time.perf_counter()
print("Took ", end_time - start_time, "seconds to generate")
if "gen_array" in locals():
start_time = time.perf_counter()
if use_palette:
create_image(palette, gen_array.reshape(width, height), prefix + "colorful_"+key, max_iter, array_top_colors, lake, shift_palette)
else:
process_image(gen_array, (2**24-1), prefix + "generated_fractal_"+key )
end_time = time.perf_counter()
del gen_array
print("Took ", end_time - start_time, "seconds to convert\n")
def generate_wrapper(n_coordinates, zoom, max_zoom, max_iter, xmin, xmax, ymin, ymax):
if zoom:
assert fractals["sandpile"]== False, "Error: Can't zoom on sandpile."
# The first image generated
generate(zoom, 0, n_coordinates+max_zoom, max_iter, xmin, xmax, ymin, ymax)
xmin1, xmax1, ymin1, ymax1 = xmin, xmax, ymin, ymax
for i in range(n_coordinates+max_zoom):
if (i < n_coordinates) and (n_coordinates !=False):
xmin, xmax, ymin, ymax = divide_in_squares(coordinates[:(i+1), :], xmin1, xmax1, ymin1, ymax1)
else:
x_center = (xmin + xmax) / 2
y_center = (ymin + ymax) / 2
widtho = (xmax - xmin) * per_zoom
heighto = (ymax - ymin) * per_zoom
xmin = x_center - widtho / 2
xmax = x_center + widtho / 2
ymin = y_center - heighto / 2
ymax = y_center + heighto / 2
generate(zoom, i+1, n_coordinates+max_zoom, max_iter, xmin, xmax, ymin, ymax)
else:
# Normal mode without zoom
generate(zoom, 0, max_zoom, max_iter, xmin, xmax, ymin, ymax)
# Let's Run
generate_wrapper(n_coordinates, zoom, max_zoom, max_iter, xmin, xmax, ymin, ymax)
# n_coordinates is how many times it will use the array coordinates.
def imgs_to_video(n_coordinates):
import os
import subprocess
import re
image_folder = os.getcwd()
fps = 10
frac = ["colorful_mandelbrot", "colorful_juliaset", "colorful_lyapunov"]
image_files = sorted([f for f in os.listdir(image_folder) if f.endswith('.png') and 'colorful' in f])
for i, n in enumerate(frac):
filtered_files = [f for f in image_files if n in f]
pattern = re.compile(rf".*{re.escape(n)}\.png$")
if any(pattern.match(f) for f in filtered_files):
with open('input.txt', 'w') as f:
for index, image_file in enumerate(filtered_files):
duration = 0.9 if index < n_coordinates else 0.1
f.write(f"file '{image_file}'\n")
f.write(f"duration {duration}\n")
f.write(f"file '{image_files[-1]}'\n")
subprocess.run([
'ffmpeg', '-f', 'concat', '-safe', '0', '-i', 'input.txt',
'-fps_mode', 'vfr', '-pix_fmt', 'yuv420p', '-vf', f'fps={fps}', f'video_{n}.mp4'
])
os.remove('input.txt')
print(f'\nvideo_{n}.mp4 Video Done!')
if video_out:
imgs_to_video(n_coordinates)