-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
340 lines (268 loc) · 13.5 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
import tkinter as tk
import tkinter.ttk as ttk
import typing
from functools import lru_cache
from random import random
from tkinter.colorchooser import askcolor
from tkinter.filedialog import asksaveasfilename
import glm
from PIL import Image, ImageTk, ImageDraw
__author__ = 'simens_green'
__version__ = '1.0.0'
# Имена доступных секций
# Available section names
SECTIONS = 'first', 'second', 'third', 'fourth'
# Стандандартные значения всех параметров
# Standard values of all parameters
DEFAULTS = {
'first': {'form': glm.mat2(0, 0, 0, .16), 'offset': glm.vec2(0, 0), 'probability': .01},
'second': {'form': glm.mat2(.85, .04, -.04, .85), 'offset': glm.vec2(0, 1.6), 'probability': .86},
'third': {'form': glm.mat2(-0.2, .26, .23, .4), 'offset': glm.vec2(0, 1.6), 'probability': .93},
'fourth': {'form': glm.mat2(-.15, .28, .26, .24), 'offset': glm.vec2(0, .44), 'probability': 0},
'number of points': 10 ** 5
}
# Диапазоны для шкал (в этих диапазонах картинка выглядит прилично,
# но пользователь в поле ввода может установить произвольное)
# Ranges for scales (in these ranges the picture looks decent,
# but the user can set arbitrary in the input field)
SCALE_RANGES = {
'first': {'form': (((-.5, .5), (-.5, .5)), ((-.07, .045), (-.5, .5))),
'offset': ((-1, 1), (-5, 5)), 'probability': (0, 1)},
'second': {'form': (((.45, 1), (-.5, 1.5)), ((-.3, .2), (.3, .95))),
'offset': ((-3, 1), (.2, 4)), 'probability': (0, 1)},
'third': {'form': (((-.5, .5), (-2, 2)), ((-1.5, 2), (-.28, .9))),
'offset': ((-5, 5), (-4, 4)), 'probability': (0, 1)},
'fourth': {'form': (((-.8, .7), (-1, 2)), ((-1.5, 1.5), (-.5, .8))),
'offset': ((-5, 5), (-7, 9)), 'probability': (0, 1)},
'number of points': (0, 2 * 10 ** 5)
}
@lru_cache(maxsize=1024)
def __transform(form: glm.mat2, current_point: glm.vec2, offset: glm.vec2) -> glm.vec2:
return form * current_point + offset
def fern_point_generator(number_of_points: int, options: dict):
"""
Генерирует значения точек-пикселей папоротника
Generates fern pixel point values
Args:
number_of_points (int): количество точек
options (dict): собственные опции для точек, вместо стандартных
own options for points instead of standard
Yields:
glm.vec2: точка. A point
"""
current_point = glm.vec2(0, 0)
for point_no in range(number_of_points):
value = random()
if value < options['first']['probability']:
current_point = __transform(options['first']['form'], current_point, options['first']['offset'])
elif value < options['second']['probability']:
current_point = __transform(options['second']['form'], current_point, options['second']['offset'])
elif value < options['third']['probability']:
current_point = __transform(options['third']['form'], current_point, options['third']['offset'])
else:
current_point = __transform(options['fourth']['form'], -current_point, options['fourth']['offset'])
yield current_point
def rescale_points(points: typing.Iterable, image_size: typing.Tuple[int, int]):
"""
Масштабирует точки (иначе размер папоротника очень мал и вверх ногами)
Scales points (otherwise the fern size is very small and upside down)
Args:
points (typing.Iterable[glm.vec2]): Набор точек. Iterable of points.
image_size (typing.Tuple[int, int]): Размер итогового изображения. Final image size
Yields:
glm.vec2: Scaled point
"""
factor = min(image_size) / 10.5
for point in points:
point = point * factor
yield int(point[0] + image_size[0] / 2), int(point[1])
def create_scale_and_entry_pair(root, var: tk.Variable,
scale_range: typing.Tuple[float, float],
state='normal') -> tk.Frame:
"""
Создает пару: шкала - текстовое поле
Creates a pair: Scale - Entry
Args:
root: родительский виджет для будущей пары. root widget for pair
var (tk.Variable): переменная, с которой будет синхронизирована шкала и текстовое поле. the variable with which
the scale and text box will be synchronized
scale_range (typing.Tuple[float, float]): диапазон значений шкалы.
state (str): состояние шкалы и текстового поля.
Returns:
tk.Frame: готовая пара.
"""
frame = tk.Frame(root)
scale = ttk.Scale(frame, variable=var, from_=scale_range[0], to=scale_range[1])
scale['state'] = state
scale.pack(side=tk.LEFT)
entry = ttk.Entry(frame, textvar=var, width=8)
entry['state'] = state
entry.pack(side=tk.RIGHT)
return frame
def rgb_byte_to_hex(color: typing.Tuple[int, ...]):
"""
Конвертирует RGB цвет в байтах (0..256) в HTML Hex
Converts RGB color in bytes (0..256) to HTML Hex
Args:
color (typing.Tuple[int, ...]): цвет RGB. RGB color
Returns:
str: HTML Hex
"""
return '#%.2x%.2x%.2x' % (color[0], color[1], color[2])
class App:
size = 720, 1024 # примерный размер окна в пикселях. approximate window size in pixels
main_color = (0, 255, 0) # цвет рисунка. picture color
canvas_bg = (0, 0, 0) # цвет холста. canvas color
tk_vars = {}
image = None
tk_image = None
allow_update = False
def __init__(self):
self.root = tk.Tk()
self.root.title('Barnsley\'s Fern')
self.__load_tk_vars()
self.canvas = tk.Canvas(self.root, width=self.size[0], height=self.size[0], bg=rgb_byte_to_hex(self.canvas_bg))
self.canvas.grid(row=0, column=0, rowspan=2)
self.main_controls = tk.Frame(self.root)
self.main_controls.grid(row=0, column=1)
self.__load_main_controls()
self.additional_control = tk.Frame(self.root)
self.additional_control.grid(row=1, column=1)
self.__load_additional_controls()
self.__load_image()
self.update()
def __load_tk_vars(self):
self.tk_vars = {
section: {
'form': [
[self.__gen_tk_var(DEFAULTS[section]['form'][0][0]),
self.__gen_tk_var(DEFAULTS[section]['form'][0][1])],
[self.__gen_tk_var(DEFAULTS[section]['form'][1][0]),
self.__gen_tk_var(DEFAULTS[section]['form'][1][1])],
],
'offset': [self.__gen_tk_var(DEFAULTS[section]['offset'][0]),
self.__gen_tk_var(DEFAULTS[section]['offset'][1])],
'probability': self.__gen_tk_var(DEFAULTS[section]['probability'])
}
for section in SECTIONS
}
self.tk_vars['number of points'] = self.__gen_tk_var(DEFAULTS['number of points'])
self.allow_update = True
def __load_additional_controls(self):
num_of_points_frame = tk.Frame(self.additional_control)
num_of_points_frame.pack()
tk.Label(num_of_points_frame, text='Количество точек:', font=('Arial', 14)).pack(side=tk.LEFT, padx=5)
ttk.Scale(num_of_points_frame, len_=self.size[0] / 2, variable=self.tk_vars['number of points'],
from_=SCALE_RANGES['number of points'][0],
to=SCALE_RANGES['number of points'][1]).pack(side=tk.LEFT)
ttk.Entry(num_of_points_frame, textvar=self.tk_vars['number of points']).pack(side=tk.LEFT)
buttons_frame = tk.Frame(self.additional_control)
buttons_frame.pack()
ttk.Button(buttons_frame, text='Обновить',
command=self.update).pack(side=tk.LEFT)
ttk.Button(buttons_frame, text='Восстановить стандартные значения',
command=self.set_defaults).pack(side=tk.LEFT)
ttk.Button(buttons_frame, text='Цвет заднего фона',
command=self.change_bg).pack(side=tk.LEFT)
ttk.Button(buttons_frame, text='Цвет рисунка',
command=self.change_fg).pack(side=tk.LEFT)
ttk.Button(buttons_frame, text='Сохранить изображение',
command=self.save_image).pack(side=tk.LEFT)
def __gen_tk_var(self, value: float = None):
var = tk.DoubleVar()
if value is not None:
var.set(value)
var.trace_add('write', self.update)
return var
def __form_controls(self, root, section):
frame = tk.Frame(root)
for i in range(2):
for j in range(2):
create_scale_and_entry_pair(frame, self.tk_vars[section]['form'][i][j],
SCALE_RANGES[section]['form'][i][j]).grid(row=i, column=j)
return frame
def __offset_controls(self, root, section):
frame = tk.Frame(root)
create_scale_and_entry_pair(frame, self.tk_vars[section]['offset'][0],
SCALE_RANGES[section]['offset'][0]).pack(side=tk.LEFT)
create_scale_and_entry_pair(frame, self.tk_vars[section]['offset'][1],
SCALE_RANGES[section]['offset'][1]).pack(side=tk.RIGHT)
return frame
def __probability_controls(self, root, section):
return create_scale_and_entry_pair(root, self.tk_vars[section]['probability'],
SCALE_RANGES[section]['probability'],
state='normal' if section != 'fourth' else 'disabled')
def __section_controls(self, root, section):
frame = tk.Frame(root)
self.__form_controls(frame, section).pack(pady=10)
self.__offset_controls(frame, section).pack(pady=10)
self.__probability_controls(frame, section).pack(pady=10)
return frame
def __load_main_controls(self):
for no, section in enumerate(SECTIONS):
self.__section_controls(self.main_controls, section).grid(row=no // 2, column=no % 2, pady=20, padx=20)
def __create_image(self):
return Image.new('RGBA', (self.size[0], self.size[0]), color=self.canvas_bg)
def __load_image(self):
self.image = self.__create_image()
self.tk_image = ImageTk.PhotoImage(image=self.image)
self.canvas.create_image(self.size[0] / 2 + 2, self.size[0] / 2 + 2, image=self.tk_image, tag=('image',))
def get_values(self):
values = {
section: {
'form': glm.mat2x2(self.tk_vars[section]['form'][0][0].get(), self.tk_vars[section]['form'][0][1].get(),
self.tk_vars[section]['form'][1][0].get(), self.tk_vars[section]['form'][1][1].get()
),
'offset': glm.vec2(self.tk_vars[section]['offset'][0].get(), self.tk_vars[section]['offset'][1].get()),
'probability': self.tk_vars[section]['probability'].get()
}
for section in SECTIONS
}
return values
def update(self, *args):
if self.image and self.allow_update:
self.canvas.delete('image')
self.image = self.__create_image()
draw = ImageDraw.Draw(self.image)
draw.point(list(set(rescale_points(fern_point_generator(int(self.tk_vars['number of points'].get()),
self.get_values()), self.image.size))),
fill=self.main_color)
self.tk_image = ImageTk.PhotoImage(image=self.image.rotate(180))
self.canvas.create_image(self.size[0] / 2 + 2, self.size[0] / 2 + 2, image=self.tk_image, tag=('image',))
return args
def change_bg(self):
color = tuple(map(int, askcolor(rgb_byte_to_hex(self.canvas_bg))[0]))
self.canvas_bg = color
self.canvas['bg'] = rgb_byte_to_hex(color)
self.update()
def change_fg(self):
color = tuple(map(int, askcolor(rgb_byte_to_hex(self.main_color))[0]))
self.main_color = color
self.update()
def save_image(self):
path = asksaveasfilename()
self.image.rotate(180).save(path + '.png', format='PNG')
def set_defaults(self):
self.allow_update = False
for section in SECTIONS:
for i in range(2):
for j in range(2):
self.tk_vars[section]['form'][i][j].set(DEFAULTS[section]['form'][i][j])
self.tk_vars[section]['offset'][0].set(DEFAULTS[section]['offset'][0])
self.tk_vars[section]['offset'][1].set(DEFAULTS[section]['offset'][1])
self.tk_vars[section]['probability'].set(DEFAULTS[section]['probability'])
self.tk_vars['number of points'].set(DEFAULTS['number of points'])
self.allow_update = True
self.update()
def run(self):
"""
Запуск приложения
Run this application
Returns:
None
"""
self.root.mainloop()
# Точка входа. Entry point
if __name__ == '__main__':
app = App()
app.run()