-
Notifications
You must be signed in to change notification settings - Fork 1
/
crop.py
123 lines (98 loc) · 3.24 KB
/
crop.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
from PIL import Image
ORANGE = (255, 102, 0)
WHITE = (255, 255, 255)
def parse_pair(im):
pix = im.load()
width, height = im.size
div_line_bottom = -1
div_line_left = -1
div_line_right = -1
div_line_top = -1
for y in range(height - 1, -1, -1):
for x in range((width // 2) - 20, (width // 2) + 20):
val = pix[x, y]
if div_line_left == -1 and val == ORANGE:
div_line_left = x
div_line_bottom = y
elif div_line_left != -1 and val != ORANGE:
div_line_right = x - 1
break
if div_line_left != -1:
break
div_line_mid = (div_line_left + div_line_right) // 2
for y in range(div_line_bottom, -1, -1):
if pix[div_line_mid, y] != ORANGE:
div_line_top = y + 1
break
assert(div_line_bottom != -1)
assert(div_line_left != -1)
assert(div_line_right != -1)
assert(div_line_top != -1)
left = im.crop((0, div_line_top - 60, div_line_left - 1, div_line_bottom))
right = im.crop((div_line_right + 1, div_line_top - 60, width, div_line_bottom))
return left, right
def parse_single(im):
pix = im.load()
width, height = im.size
left_boundary = -1
right_boundary = -1
bottom_boundary = -1
top_boundary = -1
for x in range(width - 1):
val1 = pix[x, height // 2]
val2 = pix[x + 1, height // 2]
if val1 != WHITE and val2 == WHITE:
left_boundary = x + 1
break
assert(left_boundary != -1)
for x in range(width - 1, 0, -1):
val1 = pix[x, height // 2]
val2 = pix[x - 1, height // 2]
if val1 != WHITE and val2 == WHITE:
right_boundary = x - 1
break
assert(right_boundary != -1)
for y in range(height // 2, height):
if pix[left_boundary - 1, y] == WHITE:
bottom_boundary = y
break
assert(bottom_boundary != -1)
for y in range(height // 2, -1, -1):
if pix[left_boundary - 1, y] == WHITE:
top_boundary = y
break
assert(top_boundary != -1)
return im.crop((left_boundary, top_boundary - 60, right_boundary, bottom_boundary))
def crop_sides(im, margin = 50):
width, height = im.size
pix = im.load()
content_left = -1
content_right = -1
for x in range(width):
if any(pix[x, y] != WHITE for y in range(height)):
content_left = x
break
assert(content_left != -1)
for x in range(width - 1, -1, -1):
if any(pix[x, y] != WHITE for y in range(height)):
content_right = x
break
assert(content_right != -1)
margin = min(margin, content_left, width - content_right)
return im.crop((content_left - margin, 0, content_right + margin, height))
im1 = Image.open("menu-1.png")
im2 = Image.open("menu-2.png")
im3 = Image.open("menu-3.png")
mon, tues = parse_pair(im1)
wed, thurs = parse_pair(im2)
fri = parse_single(im3)
mon = crop_sides(mon)
tues = crop_sides(tues)
wed = crop_sides(wed)
thurs = crop_sides(thurs)
fri = crop_sides(fri)
mon.save("Monday.png", "PNG")
tues.save("Tuesday.png", "PNG")
wed.save("Wednesday.png", "PNG")
thurs.save("Thursday.png", "PNG")
fri.save("Friday.png", "PNG")