-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
775 lines (636 loc) · 33.5 KB
/
script.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
import random
from PIL import Image, ImageDraw
import math
import os
def create_background_canvas(width=128, height=128, background_color='white'):
canvas = Image.new('RGB', (width, height), background_color)
return canvas
def rule1(canvas):
colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in range(5)]
first_color, *colors = colors
amount_of_first_color = round((canvas.size[0] * canvas.size[1])/random.randint(5, 8)*random.randint(1, 2))
first_positions = set()
while len(first_positions) < amount_of_first_color:
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
first_positions.add((x, y))
canvas.putpixel((x, y), first_color)
list_of_first_positions = list(first_positions)
subrules = ["LEFT", "RIGHT", "ABOVE", "BELOW"]
random_subrule = random.choice(subrules)
if random_subrule == 'ABOVE':
list_of_above_first_positions = list()
for pos in list_of_first_positions:
x, y = pos
if y > 0:
above_pos = (x, y - 1)
if above_pos[1] >= 0 and above_pos not in list_of_first_positions:
list_of_above_first_positions.append(above_pos)
random_distance = random.randint(1, 2)
for i in range(1, random_distance + 1):
left_pos = (x - i, y - 1)
right_pos = (x + i, y - 1)
if (left_pos[0] >= 0 and left_pos[0] < canvas.size[0] and
left_pos[1] >= 0 and left_pos[1] < canvas.size[1] and
left_pos not in list_of_first_positions):
list_of_above_first_positions.append(left_pos)
if (right_pos[0] >= 0 and right_pos[0] < canvas.size[0] and
right_pos[1] >= 0 and right_pos[1] < canvas.size[1] and
right_pos not in list_of_first_positions):
list_of_above_first_positions.append(right_pos)
for i in list_of_above_first_positions:
canvas.putpixel(i, colors[1])
if random_subrule == 'BELOW':
list_of_below_first_positions = list()
for pos in list_of_first_positions:
x, y = pos
if y < canvas.size[1] - 1:
below_pos = (x, y + 1)
if below_pos[1] < canvas.size[1] and below_pos not in list_of_first_positions:
list_of_below_first_positions.append(below_pos)
random_distance = random.randint(1, 2)
for i in range(1, random_distance + 1):
left_pos = (x - i, y + 1)
right_pos = (x + i, y + 1)
if (left_pos[0] >= 0 and left_pos[0] < canvas.size[0] and
left_pos[1] >= 0 and left_pos[1] < canvas.size[1] and
left_pos not in list_of_first_positions):
list_of_below_first_positions.append(left_pos)
if (right_pos[0] >= 0 and right_pos[0] < canvas.size[0] and
right_pos[1] >= 0 and right_pos[1] < canvas.size[1] and
right_pos not in list_of_first_positions):
list_of_below_first_positions.append(right_pos)
for i in list_of_below_first_positions:
canvas.putpixel(i, colors[1])
if random_subrule == 'RIGHT':
list_of_right_first_positions = list()
for pos in list_of_first_positions:
x, y = pos
if x < canvas.size[0] - 1:
right_pos = (x + 1, y)
if right_pos[0] < canvas.size[0] and right_pos not in list_of_first_positions:
list_of_right_first_positions.append(right_pos)
random_distance = random.randint(1, 2)
for i in range(1, random_distance + 1):
above_pos = (x + 1, y - i)
below_pos = (x + 1, y + i)
if (above_pos[0] < canvas.size[0] and above_pos[0] >= 0 and
above_pos[1] < canvas.size[1] and above_pos[1] >= 0 and
above_pos not in list_of_first_positions):
list_of_right_first_positions.append(above_pos)
if (below_pos[0] < canvas.size[0] and below_pos[0] >= 0 and
below_pos[1] < canvas.size[1] and below_pos[1] >= 0 and
below_pos not in list_of_first_positions):
list_of_right_first_positions.append(below_pos)
for i in list_of_right_first_positions:
canvas.putpixel(i, colors[1])
if random_subrule == 'LEFT':
list_of_left_first_positions = list()
for pos in list_of_first_positions:
x, y = pos
if x > 0:
left_pos = (x - 1, y)
if left_pos[0] >= 0 and left_pos not in list_of_first_positions:
list_of_left_first_positions.append(left_pos)
random_distance = random.randint(1, 2)
for i in range(1, random_distance + 1):
above_pos = (x - 1, y - i)
below_pos = (x - 1, y + i)
if (above_pos[0] >= 0 and above_pos[0] < canvas.size[0] and
above_pos[1] >= 0 and above_pos[1] < canvas.size[1] and
above_pos not in list_of_first_positions):
list_of_left_first_positions.append(above_pos)
if (below_pos[0] >= 0 and below_pos[0] < canvas.size[0] and
below_pos[1] >= 0 and below_pos[1] < canvas.size[1] and
below_pos not in list_of_first_positions):
list_of_left_first_positions.append(below_pos)
for i in list_of_left_first_positions:
canvas.putpixel(i, colors[1])
amount_of_third_color = round((canvas.size[0] * canvas.size[1])/random.randint(4, 6)*random.randint(1, 2))
third_positions = set()
max_iterations = canvas.size[0] * canvas.size[1] # Maximum number of iterations
iteration_count = 0
while len(third_positions) < amount_of_third_color and iteration_count < max_iterations:
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
if canvas.getpixel((x, y)) == (255, 255, 255):
third_positions.add((x, y))
canvas.putpixel((x, y), colors[2])
iteration_count += 1
try:
for x in range(canvas.size[0]):
for y in range(canvas.size[1]):
if canvas.getpixel((x, y)) == (255, 255, 255):
canvas.putpixel((x, y), colors[3])
except KeyboardInterrupt:
print("Program interrupted by the user.")
def rule2(canvas):
colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in range(5)]
first_color, *colors = colors
amount_of_first_color = round((canvas.size[0] * canvas.size[1]) / random.randint(4, 7) * random.randint(1, 3))
first_positions = set()
while len(first_positions) < amount_of_first_color:
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
first_positions.add((x, y))
canvas.putpixel((x, y), first_color)
list_of_first_positions = list(first_positions)
subrules = ["DIAGONAL_UP_RIGHT", "DIAGONAL_UP_LEFT", "DIAGONAL_DOWN_RIGHT", "DIAGONAL_DOWN_LEFT"]
random_subrule = random.choice(subrules)
if random_subrule == 'DIAGONAL_UP_RIGHT':
list_of_diagonal_up_right_positions = list()
for pos in list_of_first_positions:
x, y = pos
if x < canvas.size[0] - 1 and y > 0:
diagonal_pos = (x + 1, y - 1)
if diagonal_pos[0] < canvas.size[0] and diagonal_pos[1] >= 0 and diagonal_pos not in list_of_first_positions:
list_of_diagonal_up_right_positions.append(diagonal_pos)
for i in list_of_diagonal_up_right_positions:
canvas.putpixel(i, colors[0])
if random_subrule == 'DIAGONAL_UP_LEFT':
list_of_diagonal_up_left_positions = list()
for pos in list_of_first_positions:
x, y = pos
if x > 0 and y > 0:
diagonal_pos = (x - 1, y - 1)
if diagonal_pos[0] >= 0 and diagonal_pos[1] >= 0 and diagonal_pos not in list_of_first_positions:
list_of_diagonal_up_left_positions.append(diagonal_pos)
for i in list_of_diagonal_up_left_positions:
canvas.putpixel(i, colors[0])
if random_subrule == 'DIAGONAL_DOWN_LEFT':
list_of_diagonal_down_left_positions = list()
for pos in list_of_first_positions:
x, y = pos
if x > 0 and y < canvas.size[1] - 1:
diagonal_pos = (x - 1, y + 1)
if diagonal_pos[0] >= 0 and diagonal_pos[1] < canvas.size[1] and diagonal_pos not in list_of_first_positions:
list_of_diagonal_down_left_positions.append(diagonal_pos)
for i in list_of_diagonal_down_left_positions:
canvas.putpixel(i, colors[0])
for x in range(canvas.size[0]):
for y in range(canvas.size[1]):
if canvas.getpixel((x, y)) == (255, 255, 255):
canvas.putpixel((x, y), colors[1])
noise_positions = set()
max_noise_iterations = round((canvas.size[0] * canvas.size[1]) / random.randint(10, 20))
while len(noise_positions) < max_noise_iterations:
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
if (x, y) not in noise_positions:
noise_positions.add((x, y))
canvas.putpixel((x, y), colors[2])
border_color = colors[3]
for x in range(canvas.size[0]):
canvas.putpixel((x, 0), border_color)
canvas.putpixel((x, canvas.size[1] - 1), border_color)
for y in range(canvas.size[1]):
canvas.putpixel((0, y), border_color)
canvas.putpixel((canvas.size[0] - 1, y), border_color)
def rule3(canvas):
colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in range(5)]
first_color, *colors = colors
amount_of_first_color = round((canvas.size[0] * canvas.size[1])/random.randint(5, 8)*random.randint(1, 2))
first_positions = set()
while len(first_positions) < amount_of_first_color:
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
first_positions.add((x, y))
canvas.putpixel((x, y), first_color)
list_of_first_positions = list(first_positions)
subrules = ["DIAGONAL_UP_LEFT", "DIAGONAL_UP_RIGHT", "DIAGONAL_DOWN_LEFT", "DIAGONAL_DOWN_RIGHT"]
random_subrule = random.choice(subrules)
if random_subrule == 'DIAGONAL_UP_LEFT':
list_of_diagonal_up_left_positions = list()
for pos in list_of_first_positions:
x, y = pos
if x > 0 and y > 0:
diagonal_up_left_pos = (x - 1, y - 1)
if diagonal_up_left_pos[0] >= 0 and diagonal_up_left_pos[1] >= 0 and diagonal_up_left_pos not in list_of_first_positions:
list_of_diagonal_up_left_positions.append(diagonal_up_left_pos)
random_distance = random.randint(1, 2)
for i in range(1, random_distance + 1):
up_left_pos = (x - i, y - i)
if (up_left_pos[0] >= 0 and up_left_pos[0] < canvas.size[0] and
up_left_pos[1] >= 0 and up_left_pos[1] < canvas.size[1] and
up_left_pos not in list_of_first_positions):
list_of_diagonal_up_left_positions.append(up_left_pos)
for i in list_of_diagonal_up_left_positions:
canvas.putpixel(i, colors[1])
if random_subrule == 'DIAGONAL_UP_RIGHT':
list_of_diagonal_up_right_positions = list()
for pos in list_of_first_positions:
x, y = pos
if x < canvas.size[0] - 1 and y > 0:
diagonal_up_right_pos = (x + 1, y - 1)
if diagonal_up_right_pos[0] < canvas.size[0] and diagonal_up_right_pos[1] >= 0 and diagonal_up_right_pos not in list_of_first_positions:
list_of_diagonal_up_right_positions.append(diagonal_up_right_pos)
random_distance = random.randint(1, 2)
for i in range(1, random_distance + 1):
up_right_pos = (x + i, y - i)
if (up_right_pos[0] < canvas.size[0] and up_right_pos[0] >= 0 and
up_right_pos[1] >= 0 and up_right_pos[1] < canvas.size[1] and
up_right_pos not in list_of_first_positions):
list_of_diagonal_up_right_positions.append(up_right_pos)
for i in list_of_diagonal_up_right_positions:
canvas.putpixel(i, colors[1])
if random_subrule == 'DIAGONAL_DOWN_LEFT':
list_of_diagonal_down_left_positions = list()
for pos in list_of_first_positions:
x, y = pos
if x > 0 and y < canvas.size[1] - 1:
diagonal_down_left_pos = (x - 1, y + 1)
if diagonal_down_left_pos[0] >= 0 and diagonal_down_left_pos[1] < canvas.size[1] and diagonal_down_left_pos not in list_of_first_positions:
list_of_diagonal_down_left_positions.append(diagonal_down_left_pos)
random_distance = random.randint(1, 2)
for i in range(1, random_distance + 1):
down_left_pos = (x - i, y + i)
if (down_left_pos[0] >= 0 and down_left_pos[0] < canvas.size[0] and
down_left_pos[1] >= 0 and down_left_pos[1] < canvas.size[1] and
down_left_pos not in list_of_first_positions):
list_of_diagonal_down_left_positions.append(down_left_pos)
for i in list_of_diagonal_down_left_positions:
canvas.putpixel(i, colors[1])
if random_subrule == 'DIAGONAL_DOWN_RIGHT':
list_of_diagonal_down_right_positions = list()
for pos in list_of_first_positions:
x, y = pos
if x < canvas.size[0] - 1 and y < canvas.size[1] - 1:
diagonal_down_right_pos = (x + 1, y + 1)
if diagonal_down_right_pos[0] < canvas.size[0] and diagonal_down_right_pos[1] < canvas.size[1] and diagonal_down_right_pos not in list_of_first_positions:
list_of_diagonal_down_right_positions.append(diagonal_down_right_pos)
random_distance = random.randint(1, 2)
for i in range(1, random_distance + 1):
down_right_pos = (x + i, y + i)
if (down_right_pos[0] < canvas.size[0] and down_right_pos[0] >= 0 and
down_right_pos[1] < canvas.size[1] and down_right_pos[1] >= 0 and
down_right_pos not in list_of_first_positions):
list_of_diagonal_down_right_positions.append(down_right_pos)
for i in list_of_diagonal_down_right_positions:
canvas.putpixel(i, colors[1])
amount_of_third_color = round((canvas.size[0] * canvas.size[1])/random.randint(4, 6)*random.randint(1, 2))
third_positions = set()
max_iterations = canvas.size[0] * canvas.size[1] # Maximum number of iterations
iteration_count = 0
while len(third_positions) < amount_of_third_color and iteration_count < max_iterations:
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
if canvas.getpixel((x, y)) == (255, 255, 255):
third_positions.add((x, y))
canvas.putpixel((x, y), colors[2])
iteration_count += 1
try:
for x in range(canvas.size[0]):
for y in range(canvas.size[1]):
if canvas.getpixel((x, y)) == (255, 255, 255):
canvas.putpixel((x, y), colors[3])
except KeyboardInterrupt:
print("Program interrupted by the user.")
def rule4(canvas):
colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in range(5)]
first_color, *colors = colors
amount_of_first_color = round((canvas.size[0] * canvas.size[1])/random.randint(3, 5)*random.randint(1, 2))
first_positions = set()
while len(first_positions) < amount_of_first_color:
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
first_positions.add((x, y))
canvas.putpixel((x, y), first_color)
list_of_first_positions = list(first_positions)
subrules = ["EXPLOSION", "SPIRAL", "RIPPLE", "GALAXY"]
random_subrule = random.choice(subrules)
if random_subrule == 'EXPLOSION':
list_of_explosion_positions = list()
for pos in list_of_first_positions:
x, y = pos
for i in range(1, random.randint(2, 5)):
explosion_pos = (x + random.randint(-i, i), y + random.randint(-i, i))
if (explosion_pos[0] >= 0 and explosion_pos[0] < canvas.size[0] and
explosion_pos[1] >= 0 and explosion_pos[1] < canvas.size[1] and
explosion_pos not in list_of_first_positions):
list_of_explosion_positions.append(explosion_pos)
for i in list_of_explosion_positions:
canvas.putpixel((int(i[0]), int(i[1])), colors[1])
if random_subrule == 'SPIRAL':
list_of_spiral_positions = list()
for pos in list_of_first_positions:
x, y = pos
for i in range(1, random.randint(2, 5)):
spiral_pos = (x + i * math.cos(i * math.pi / 4), y + i * math.sin(i * math.pi / 4))
if (spiral_pos[0] >= 0 and spiral_pos[0] < canvas.size[0] and
spiral_pos[1] >= 0 and spiral_pos[1] < canvas.size[1] and
spiral_pos not in list_of_first_positions):
list_of_spiral_positions.append(spiral_pos)
for i in list_of_spiral_positions:
canvas.putpixel((int(i[0]), int(i[1])), colors[1])
if random_subrule == 'RIPPLE':
list_of_ripple_positions = list()
for pos in list_of_first_positions:
x, y = pos
for i in range(1, random.randint(2, 5)):
ripple_pos = (x + i * math.cos(i * math.pi / 2), y + i * math.sin(i * math.pi / 2))
if (ripple_pos[0] >= 0 and ripple_pos[0] < canvas.size[0] and
ripple_pos[1] >= 0 and ripple_pos[1] < canvas.size[1] and
ripple_pos not in list_of_first_positions):
list_of_ripple_positions.append(ripple_pos)
for i in list_of_ripple_positions:
canvas.putpixel((int(i[0]), int(i[1])), colors[1])
if random_subrule == 'GALAXY':
list_of_galaxy_positions = list()
for pos in list_of_first_positions:
x, y = pos
for i in range(1, random.randint(2, 5)):
galaxy_pos = (x + i * math.cos(i * math.pi / 3), y + i * math.sin(i * math.pi / 3))
if (galaxy_pos[0] >= 0 and galaxy_pos[0] < canvas.size[0] and
galaxy_pos[1] >= 0 and galaxy_pos[1] < canvas.size[1] and
galaxy_pos not in list_of_first_positions):
list_of_galaxy_positions.append(galaxy_pos)
for i in list_of_galaxy_positions:
canvas.putpixel((int(i[0]), int(i[1])), colors[1])
amount_of_third_color = round((canvas.size[0] * canvas.size[1])/random.randint(4, 6)*random.randint(1, 2))
third_positions = set()
max_iterations = canvas.size[0] * canvas.size[1] # Maximum number of iterations
iteration_count = 0
while len(third_positions) < amount_of_third_color and iteration_count < max_iterations:
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
if canvas.getpixel((x, y)) == (255, 255, 255):
third_positions.add((x, y))
canvas.putpixel((x, y), colors[2])
iteration_count += 1
try:
for x in range(canvas.size[0]):
for y in range(canvas.size[1]):
if canvas.getpixel((x, y)) == (255, 255, 255):
canvas.putpixel((x, y), colors[3])
except KeyboardInterrupt:
print("Program interrupted by the user.")
def rule5(canvas):
colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in range(7)]
primary_color = colors[0]
secondary_color = colors[1]
num_seed_pixels = round((canvas.size[0] * canvas.size[1]) / random.randint(10, 20))
seed_pixels = set()
while len(seed_pixels) < num_seed_pixels:
x = random.randint(0, canvas.size[0] - 1)
y = random.randint(0, canvas.size[1] - 1)
seed_pixels.add((x, y))
canvas.putpixel((x, y), primary_color)
num_iterations = random.randint(3, 6)
for _ in range(num_iterations):
new_pixels = set()
for x in range(canvas.size[0]):
for y in range(canvas.size[1]):
if (x, y) not in seed_pixels:
neighbor_count = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if (x + dx, y + dy) in seed_pixels:
neighbor_count += 1
if neighbor_count >= 2:
new_pixels.add((x, y))
canvas.putpixel((x, y), secondary_color)
seed_pixels.update(new_pixels)
for x in range(canvas.size[0]):
for y in range(canvas.size[1]):
if (x, y) in seed_pixels:
neighbor_count_primary = 0
neighbor_count_secondary = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if (x + dx, y + dy) in seed_pixels:
if canvas.getpixel((x + dx, y + dy)) == primary_color:
neighbor_count_primary += 1
elif canvas.getpixel((x + dx, y + dy)) == secondary_color:
neighbor_count_secondary += 1
if neighbor_count_primary >= 2 and neighbor_count_secondary >= 2:
canvas.putpixel((x, y), random.choice(colors[2:]))
for x in range(canvas.size[0]):
for y in range(canvas.size[1]):
if canvas.getpixel((x, y)) == (255, 255, 255):
canvas.putpixel((x, y), random.choice(colors))
def rule6(canvas):
colors = [(random.randint(0, 1) * 255, random.randint(0, 1) * 255, random.randint(0, 1) * 255) for _ in range(5)]
first_color, *colors = colors
amount_of_first_color = round((canvas.size[0] * canvas.size[1]) / random.randint(5, 8) * random.randint(1, 2))
first_positions = set()
while len(first_positions) < amount_of_first_color:
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
first_positions.add((x, y))
canvas.putpixel((x, y), first_color)
list_of_first_positions = list(first_positions)
submodes = ["DIAGONAL_UP_RIGHT", "DIAGONAL_UP_LEFT", "DIAGONAL_DOWN_RIGHT", "DIAGONAL_DOWN_LEFT", "CROSS", "SQUARE"]
random_submode = random.choice(submodes)
if random_submode == "DIAGONAL_UP_RIGHT":
list_of_diagonal_up_right_positions = []
for pos in list_of_first_positions:
x, y = pos
if x < canvas.size[0] - 1 and y > 0:
diagonal_pos = (x + 1, y - 1)
if diagonal_pos not in list_of_first_positions:
list_of_diagonal_up_right_positions.append(diagonal_pos)
for pos in list_of_diagonal_up_right_positions:
canvas.putpixel(pos, colors[1])
elif random_submode == "DIAGONAL_UP_LEFT":
list_of_diagonal_up_left_positions = []
for pos in list_of_first_positions:
x, y = pos
if x > 0 and y > 0:
diagonal_pos = (x - 1, y - 1)
if diagonal_pos not in list_of_first_positions:
list_of_diagonal_up_left_positions.append(diagonal_pos)
for pos in list_of_diagonal_up_left_positions:
canvas.putpixel(pos, colors[1])
elif random_submode == "DIAGONAL_DOWN_RIGHT":
list_of_diagonal_down_right_positions = []
for pos in list_of_first_positions:
x, y = pos
if x < canvas.size[0] - 1 and y < canvas.size[1] - 1:
diagonal_pos = (x + 1, y + 1)
if diagonal_pos not in list_of_first_positions:
list_of_diagonal_down_right_positions.append(diagonal_pos)
for pos in list_of_diagonal_down_right_positions:
canvas.putpixel(pos, colors[1])
elif random_submode == "DIAGONAL_DOWN_LEFT":
list_of_diagonal_down_left_positions = []
for pos in list_of_first_positions:
x, y = pos
if x > 0 and y < canvas.size[1] - 1:
diagonal_pos = (x - 1, y + 1)
if diagonal_pos not in list_of_first_positions:
list_of_diagonal_down_left_positions.append(diagonal_pos)
for pos in list_of_diagonal_down_left_positions:
canvas.putpixel(pos, colors[1])
elif random_submode == "CROSS":
list_of_cross_positions = []
for pos in list_of_first_positions:
x, y = pos
cross_positions = [(x, y - 1), (x, y + 1), (x - 1, y), (x + 1, y)]
for cross_pos in cross_positions:
if (
0 <= cross_pos[0] < canvas.size[0]
and 0 <= cross_pos[1] < canvas.size[1]
and cross_pos not in list_of_first_positions
):
list_of_cross_positions.append(cross_pos)
for pos in list_of_cross_positions:
canvas.putpixel(pos, colors[1])
elif random_submode == "SQUARE":
list_of_square_positions = []
for pos in list_of_first_positions:
x, y = pos
square_positions = [
(x - 1, y - 1),
(x, y - 1),
(x + 1, y - 1),
(x - 1, y),
(x + 1, y),
(x - 1, y + 1),
(x, y + 1),
(x + 1, y + 1),
]
for square_pos in square_positions:
if (
0 <= square_pos[0] < canvas.size[0]
and 0 <= square_pos[1] < canvas.size[1]
and square_pos not in list_of_first_positions
):
list_of_square_positions.append(square_pos)
for pos in list_of_square_positions:
canvas.putpixel(pos, colors[1])
amount_of_third_color = round((canvas.size[0] * canvas.size[1]) / random.randint(4, 6) * random.randint(1, 2))
third_positions = set()
max_iterations = canvas.size[0] * canvas.size[1] # Maximum number of iterations
iteration_count = 0
while len(third_positions) < amount_of_third_color and iteration_count < max_iterations:
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
if canvas.getpixel((x, y)) == (255, 255, 255):
third_positions.add((x, y))
canvas.putpixel((x, y), colors[2])
iteration_count += 1
for x in range(canvas.size[0]):
for y in range(canvas.size[1]):
if canvas.getpixel((x, y)) == (255, 255, 255):
canvas.putpixel((x, y), colors[3])
def rule7(canvas):
colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in range(4)]
first_positions = set()
amount_of_first_color = round((canvas.size[0] * canvas.size[1])/random.randint(5, 8)*random.randint(1, 2))
for _ in range(amount_of_first_color):
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
first_positions.add((x, y))
canvas.putpixel((x, y), colors[0])
above_first_positions = []
for pos in first_positions:
x, y = pos
if y > 0:
above_pos = (x, y - 1)
if above_pos[1] >= 0 and above_pos not in first_positions:
above_first_positions.append(above_pos)
below_first_positions = []
for pos in first_positions:
x, y = pos
if y < canvas.size[1] - 1:
below_pos = (x, y + 1)
if below_pos[1] < canvas.size[1] and below_pos not in first_positions:
below_first_positions.append(below_pos)
right_first_positions = []
for pos in first_positions:
x, y = pos
if x < canvas.size[0] - 1:
right_pos = (x + 1, y)
if right_pos[0] < canvas.size[0] and right_pos not in first_positions:
right_first_positions.append(right_pos)
left_first_positions = []
for pos in first_positions:
x, y = pos
if x > 0:
left_pos = (x - 1, y)
if left_pos[0] >= 0 and left_pos not in first_positions:
left_first_positions.append(left_pos)
for pos in above_first_positions:
canvas.putpixel(pos, colors[1])
for pos in below_first_positions:
canvas.putpixel(pos, colors[1])
for pos in right_first_positions:
canvas.putpixel(pos, colors[1])
for pos in left_first_positions:
canvas.putpixel(pos, colors[1])
third_positions = set()
amount_of_third_color = round((canvas.size[0] * canvas.size[1])/random.randint(4, 6)*random.randint(1, 2))
for _ in range(amount_of_third_color):
x, y = random.randint(0, canvas.size[0] - 1), random.randint(0, canvas.size[1] - 1)
if canvas.getpixel((x, y)) == (255, 255, 255):
third_positions.add((x, y))
canvas.putpixel((x, y), colors[2])
fourth_positions = set()
for x in range(canvas.size[0]):
for y in range(canvas.size[1]):
if canvas.getpixel((x, y)) == (255, 255, 255):
fourth_positions.add((x, y))
canvas.putpixel((x, y), colors[3])
return canvas
# This part of code for getting all the images pixels pixel by pixel and their positions
# def generate_pixel_matrix(canvas):
# width, height = canvas.size
# pixel_matrix = []
# for y in range(height):
# row = []
# for x in range(width):
# color = canvas.getpixel((x, y))
# row.append(((x, y), color))
# pixel_matrix.append(row)
# return pixel_matrix
# pixel_matrix = generate_pixel_matrix(some_canvas)
# Single generaion
# resize is using becouse it's faster and easier to generate an image
# of smaller size and then upscale it and sometime less is more
# c_width, c_height = 124, 68
# some_canvas = create_background_canvas(c_width, c_height, 'white')
# rule3(some_canvas)
# scale_factor = round(1980 / c_width) # Adjust the scale factor as needed for FULL HD size
# resized_image = some_canvas.resize((some_canvas.width * scale_factor, some_canvas.height * scale_factor), resample=Image.NEAREST)
# # Save the resized image
# resized_image.show()
rules = [rule1, rule2, rule3, rule4, rule5, rule6, rule7]
# Set the output directory
output_dir = "output"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# # Set the input and output directories
# input_dir = "output"
# if not os.path.exists(output_dir):
# os.makedirs(output_dir)
def generate_images(amount, width, height, rule_index):
for i in range(amount):
canvas = create_background_canvas(width, height, 'white')
rules[rule_index](canvas)
scale_factor = round(1980 / width)
canvas = canvas.resize((canvas.width * scale_factor, canvas.height * scale_factor), resample=Image.NEAREST)
filename = f"{i+1}_{width}x{height}_{rule_index+1}.png"
output_path = os.path.join(output_dir, filename)
canvas.save(output_path)
print(f"Image '{filename}' saved successfully.")
# # For generation using script instead of GUI
# rules = [rule1, rule2, rule3, rule4, rule5, rule6, rule7]
# width_height = [(31, 17), (62, 34), (93, 51), (124, 68)]
# rules = [rule2]
# width_height = [(16, 16)]
# # Generate any amount of images with different rules and sizes
# for i in range(10):
# # Choose a random rule
# rule = random.choice(rules)
# # rule = rules[0] # Coose a single rule
# # Choose a set of sizes randomly
# size = random.choice(width_height)
# width = size[0]
# height = size[1]
# # Or put the width and size manually
# # width = 62
# # size = 34
# # Create a new canvas
# canvas = create_background_canvas(width, height, 'white')
# # Apply the chosen rule
# rule(canvas)
# # Apply the scaling rule if needed
# scale_factor = round(1980 / width) # Adjust the scale factor as needed for FULL HD size
# canvas = canvas.resize((canvas.width * scale_factor, canvas.height * scale_factor), resample=Image.NEAREST)
# # Generate a unique filename
# rule_index = rules.index(rule) + 1 # Rules are indexed from 1
# unique_number = random.randint(1, 9999)
# filename = f"{unique_number}_{width}x{height}_{rule_index}.png"
# output_path = os.path.join(output_dir, filename)
# # Save the image
# canvas.save(output_path)
# print(f"Image '{filename}' saved successfully.")