-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
349 lines (291 loc) · 8.77 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
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
import json
import os
import random as rand
from math import sin, tan
from typing import Callable, List, Tuple
import numpy as np
from a import generate_img
from laconcha import ColorChannel, Image
from laconcha.curves import (circular, cosine, cubic, exponential, inverse,
logarithm, quadratic, sine, smooth_step,
smoother_step, smoothest_step, square_root)
from laconcha.filters import (ColorMode, autocontrast, bilateral_filter,
bottom, brightness, color_quantization, contrast,
convert_color, crop, curve, equalize, fit,
gaussian_blur, get_channel, hflip, hmirror,
integral, invert, left, max_filter, mean_filter,
median_filter, min_filter, mode_filter,
oct_mirror, posterize, quad_mirror, right,
rotate, saturation, scale, sharpness, shear,
shuffle, solarize, spread, swirl, swirl_flower,
top, transform, translate, unsharpen, vflip,
vmirror)
from laconcha.generators import (from_function, from_rgb_functions,
gaussian_noise, maurer_rose, solid_color,
white_noise)
from laconcha.operators import (add, add_modulo, blend, darker, difference,
hard_light, lighter, overlay, screen,
set_channel, soft_light, subtract,
subtract_modulo)
from laconcha.seed import get_seed
from laconcha.util import hex_to_rgb
HEIGHT, WIDTH = 1024, 1024
# HEIGHT, WIDTH = 400, 400
seed = input('Seed? ')
if seed == '':
seed = get_seed()
print(f'Seed: {seed}')
rand.seed(seed)
MIN_DEPTH = 1
MAX_DEPTH = 5
functions = [
lambda x, y: 255 * (sin(x) / 2 + .5),
lambda x, y: 255 * (sin(y) / 2 + .5),
lambda x, y: 255 * (sin(x) / 2 + .5) * (sin(y) / 2 + .5),
lambda x, y: x**2 % 255,
lambda x, y: y**2 % 255,
lambda x, y: (x**2 + y**2) % 255,
lambda x, y: tan(x) % 255,
lambda x, y: tan(y) % 255,
lambda x, y: tan(x) % 255 + tan(y) % 255,
]
def function(size, _):
return from_function(rand.choice(functions))(size)
def rgb_function(size, _):
return from_rgb_functions(*rand.choices(functions, k=3))(size)
files = [f for f in os.listdir('test_images') if f.endswith('.jpg')]
def file(size, _):
return fit(size)(Image.open('test_images/' + rand.choice(files)))
# TODO: Weight choices
generators = [
# file,
# function,
# rgb_function,
# gaussian_noise,
# white_noise
(1, solid_color, lambda: [
rand.choices(range(256), k=3)
])
(2, maurer_rose, lambda: [
*rand.choice([
(2, 39),
(3, 47),
(4, 31),
(5, 97),
(6, 71),
(7, 19)
]),
rand.randint(min(HEIGHT, WIDTH) // 2, max(HEIGHT, WIDTH)),
rand.choices(range(256), k=3),
rand.choices(range(256), k=3)
]),
(3, lambda: generate_img, lambda: [])
]
weights = [i[0] for i in generators]
generators = [i[1:] for i in generators]
filters: List[Tuple[Callable, Callable]] = [
(autocontrast, lambda h, w: [
rand.randint(0, 49)
]),
(bilateral_filter, lambda h, w: [
rand.randint(1, 15),
rand.randint(0, 100),
rand.randint(0, 100)
]),
# (bottom, lambda h, w: [
# rand.randint(0, h)
# ]),
(brightness, lambda h, w: [
2 * rand.random()
]),
(get_channel, lambda h, w: [
rand.choice(list(ColorChannel))
]),
(color_quantization, lambda h, w: [
rand.randint(2, 16)
]),
(contrast, lambda h, w: [
2 * rand.random()
]),
(convert_color, lambda h, w: [
list(ColorMode),
list(ColorMode)
]),
# (crop, lambda h, w: [
# (rand.randint(0, h),
# rand.randint(0, w))
# ]),
(curve, lambda h, w: [
rand.choice([
circular,
cosine,
cubic,
exponential,
inverse,
logarithm,
sine,
smooth_step,
smoother_step,
smoothest_step,
square_root,
quadratic
])
]),
(equalize, lambda h, w: []),
# (fit, lambda h, w: [
# (rand.randint(0, h),
# rand.randint(0, w))
# ]),
(gaussian_blur, lambda h, w: [
(rand.randrange(1, 8, 2),
rand.randrange(1, 8, 2)),
10 * rand.random()
]),
(lambda: hflip, lambda h, w: []),
(lambda: hmirror, lambda h, w: []),
# 'Integral': (integral, lambda h, w: []),
(lambda: invert, lambda h, w: []),
# (left, lambda h, w: [
# rand.randint(0, w)
# ]),
(max_filter, lambda h, w: [
rand.randrange(1, 16, 2)
]),
(mean_filter, lambda h, w: [
(rand.randint(1, 7),
rand.randint(1, 7))
]),
(median_filter, lambda h, w: [
rand.randrange(1, 16, 2)
]),
(min_filter, lambda h, w: [
rand.randrange(1, 16, 2)
]),
(mode_filter, lambda h, w: [
rand.randrange(1, 16, 2)
]),
(lambda: oct_mirror, lambda h, w: []),
(posterize, lambda h, w: [
rand.randint(2, 8)
]),
(lambda: quad_mirror, lambda h, w: []),
# (right, lambda h, w: [
# rand.randint(0, w)
# ]),
(rotate, lambda h, w: [
360 * rand.random()
]),
(saturation, lambda h, w: [
2 * rand.random()
]),
(scale, lambda h, w: [
9 * rand.random() + 1
]),
(sharpness, lambda h, w: [
2 * rand.random()
]),
(shuffle, lambda h, w: [
rand.randint(0, int(16 * '1', base=2))
]),
(solarize, lambda h, w: [
rand.randint(0, 255)
]),
(spread, lambda h, w: [
rand.randint(10, 50)
]),
(swirl, lambda h, w: [
(w // 2, h // 2),
200 * rand.random() - 100,
max(h, w)
]),
(unsharpen, lambda h, w: [
rand.randint(1, 15)
]),
(lambda: vflip, lambda h, w: []),
(lambda: vmirror, lambda h, w: [])
]
def rblend(a, b):
return blend(rand.random())(a, b)
def rset_channel(a, b):
return set_channel(rand.choice(list(ColorChannel)))(a, b)
operators = [
(2, add()),
(2, add_modulo()),
(2, rblend),
(2, darker()),
(2, difference()),
(2, hard_light()),
(2, lighter()),
(2, overlay()),
(2, screen()),
(2, rset_channel),
(2, soft_light()),
(2, subtract()),
(2, subtract_modulo())
]
def get_image(depth: int = 1) -> Image:
if depth == 5:
return get_filtered()
if depth > 2 and rand.random() < .25:
return get_generator()
o = rand.choice(operators)
# print(o[1].__name__)
i = o[1](*[get_image(depth + 1) for _ in range(o[0])]).as_scikit()
num_colors = len(np.unique(i.reshape(i.shape[0] * i.shape[1], 3), axis=0))
if num_colors == 1:
return get_image()
return Image.from_scikit(i)
def get_filtered(depth: int = 1) -> Image:
if depth == MAX_DEPTH:
return get_generator()
if depth > MIN_DEPTH and rand.random() < .25:
return get_generator()
f = rand.choice(filters)
# print(f[0].__name__)
return f[0](*f[1](HEIGHT, WIDTH))(get_filtered(depth + 1))
def get_generator() -> Image:
g = rand.choices(generators, weights=weights)
# print(g.__name__)
return g[1](*g[2]())((HEIGHT, WIDTH))
with open('1000.json', 'r') as f:
palettes = json.load(f)
palette = rand.choice(palettes)
N = 5
# tru_img = rand.choice([
# # hmirror(),
# quad_mirror(),
# oct_mirror(),
# flower(rand.choice([
# add(),
# add_modulo(),
# darker(),
# difference(),
# hard_light(),
# lighter(),
# overlay(),
# screen(),
# soft_light(),
# subtract(),
# subtract_modulo()
# ]), 500, max(HEIGHT, WIDTH))
# ])(get_image())
tru_img = get_image()
img = tru_img.as_scikit()
colors, counts = np.unique(img.reshape(
img.shape[0] * img.shape[1], 3), return_counts=True, axis=0)
if len(colors) > N:
there = convert_color(ColorMode.RGB, ColorMode.LAB)
back = convert_color(ColorMode.LAB, ColorMode.RGB)
img = back(color_quantization(N)(there(tru_img))).as_scikit()
colors, counts = np.unique(img.reshape(
img.shape[0] * img.shape[1], 3), return_counts=True, axis=0)
indices = np.argsort(counts)
for i in range(len(colors)):
mask = np.where((img == colors[indices[i]]).all(axis=2))
img[mask] = hex_to_rgb(palette[i])
img = mode_filter(7)(Image.from_scikit(img))
# img = tru_img
img.show('Palettized')
save = input('Save? ')
if save.strip().lower() in ['y', 'yes']:
img.save(f'palettized/{seed}.jpg')