-
Notifications
You must be signed in to change notification settings - Fork 1
/
couteau.py
85 lines (64 loc) · 1.92 KB
/
couteau.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
from PIL import Image
from PIL import ImageDraw
import sys
# left, up, right, low
def extractRect(im, box, filename):
region = im.crop(box)
region.save(filename)
def floodfill(image, xy, value, border=None):
"Fill bounded region."
# based on an implementation by Eric S. Raymond
pixel = image.load()
x, y = xy
max_x = x
min_x = x
max_y = y
min_y = y
background = pixel[x, y]
if background == value:
raise ValueError()
pixel[x, y] = value
edge = [(x, y)]
while edge:
newedge = []
for (x, y) in edge:
for (s, t) in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
try:
p = pixel[s, t]
except IndexError:
pass
else:
if p == background:
max_x = max(max_x, s)
min_x = min(min_x, s)
max_y = max(max_y, t)
min_y = min(min_y, t)
pixel[s, t] = value
newedge.append((s, t))
edge = newedge
return (min_x, min_y, max_x, max_y)
def decouper(nomFichier):
point = (0, 0)
im = Image.open(nomFichier)
imOrig = im.copy()
im = im.convert("RGB")
prev_max_y = 0
prev_max_x = 0
x_inc = 4
y_inc = 4
for jour in ['titre', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi']:
prev_max_y = 0
point = (prev_max_x + x_inc, 0)
for case in range(7):
point = (point[0], prev_max_y + y_inc)
# print point
box = floodfill(im, point, (255, 0, 0))
# print box
extractRect(imOrig, box, jour + str(case) + ".bmp")
prev_max_y = box[3]
prev_max_x = box[2]
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Quel cave!")
else:
decouper(sys.argv[1])