-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheffects.py
115 lines (94 loc) · 3.54 KB
/
effects.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
from PIL import Image
from random import randint
def to_red(image, grid_canvas):
cellw = grid_canvas.cellw
cellh = grid_canvas.cellh
for i, j in grid_canvas.selection.all_positions():
x = i * cellw
y = j * cellh
box = (x, y, x + cellw, y + cellh)
image.paste(make_red(image.crop(box)), box)
return image
def to_red_checkers(image, grid_canvas):
cellw = grid_canvas.cellw
cellh = grid_canvas.cellh
for i in range(
grid_canvas.selection.get_left_boundary(), grid_canvas.selection.get_right_boundary() + 1
):
for j in range(
grid_canvas.selection.get_top_boundary(), grid_canvas.selection.get_bottom_boundary() + 1
):
if grid_canvas.selection.position_id(i, j) != None:
x = i * cellw
y = j * cellh
box = (x, y, x + cellw, y + cellh)
# one is even and the other is not
if (i % 2 == 0 and j % 2 != 0) or (i % 2 != 0 and j % 2 == 0):
image.paste(make_red(image.crop(box)), box)
return image
def crop(image):
return image.crop((0, 0, 600, 400))
def make_red(image):
imageR = image.getchannel(0)
imageN = imageR.point(lambda i: False)
return Image.merge(image.mode, [imageR, imageN, imageN])
def to_shuffle_paste(image, grid_canvas, var):
cellw = grid_canvas.cellw
cellh = grid_canvas.cellh
selected_cells = {}
for x in range(
grid_canvas.selection.get_left_boundary(), grid_canvas.selection.get_right_boundary() + 1
):
for y in range(
grid_canvas.selection.get_top_boundary(), grid_canvas.selection.get_bottom_boundary() + 1
):
if grid_canvas.selection.position_id(x, y) is not None:
selected_cells[(x, y)] = image.crop(
(x * cellw, y * cellh, x * cellw + cellw, y * cellh + cellh)
)
for pos in selected_cells.keys():
x = pos[0]
y = pos[1]
n1 = randint(-var, var)
n2 = randint(-var, var)
image.paste(selected_cells[pos], ((x + n1) * cellw, (y + n2) * cellh))
return image
def to_shuffle_swap(image, grid_canvas, var):
cwidth = grid_canvas.cellw
cheight = grid_canvas.cellh
for x in range(
grid_canvas.selection.get_left_boundary(),
grid_canvas.selection.get_right_boundary() + 1,
):
for y in range(
grid_canvas.selection.get_top_boundary(),
grid_canvas.selection.get_bottom_boundary() + 1,
):
if grid_canvas.selection.position_id(x, y) is not None:
c = 0
while c < 3:
xfound = randint(x - var, x + var)
yfound = randint(y - var, y + var)
if grid_canvas.selection.position_id(xfound, yfound) is not None:
break
c += 1
if c < 3:
ibox = (
x * cwidth,
y * cheight,
(x + 1) * cwidth,
(y + 1) * cheight,
)
guybox = (
xfound * cwidth,
yfound * cheight,
(xfound + 1) * cwidth,
(yfound + 1) * cheight,
)
image = swap(image, ibox, guybox)
return image
def swap(image, box1, box2):
buff = image.crop(box1)
image.paste(image.crop(box2), box1)
image.paste(buff, box2)
return image