-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.py
157 lines (137 loc) · 4.72 KB
/
24.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
from PIL import Image
import logging
import logging.handlers
import constant
class Way:
def __init__(self, pasts):
self.pasts = pasts
def main():
file_path = "maze/maze.png"
logger = set_logger()
points_pixels, width, height = first_step(file_path)
# second_step(points_pixels, height)
start = (639, 0)
# third_step(start, points_pixels, logger)
points = constant.points
fourth_step(points_pixels, points)
def first_step(file_path):
im = Image.open(file_path)
size = im.size
width = size[0]
height = size[1]
points_pixels = {}
for x in range(width):
for y in range(height):
points_pixels[("{}_{}".format(x, y))] = im.getpixel((x, y))
return points_pixels, width, height
def second_step(points_pixels, height):
y = 0
for x in range(height):
pixel = points_pixels["{}_{}".format(x, y)]
if pixel != (255, 255, 255, 255) and pixel != (127, 127, 127, 255):
print("x={}, y={}, pixel={}".format(x, y, pixel))
def third_step(start, points_pixels, logger):
x = start[0]
y = start[1]
pasts = [(x, y)]
ways = []
way = Way(pasts)
ways.append(way)
while True:
if len(ways) == 0:
break
else:
now_way = ways[-1]
now_way_pasts = now_way.pasts
print("len(ways)={}, now_ways_pasts={}".format(len(ways), now_way_pasts))
if len(ways) == 1:
logger.info("now_ways_pasts={}".format(now_way_pasts))
x = now_way_pasts[-1][0]
y = now_way_pasts[-1][1]
if y > 638:
logger.info("now_way={}, now_way's pasts={}".format(now_way, now_way.pasts))
if y == 640:
break
points = find_next_point(points_pixels, x, y, now_way_pasts, logger)
if points is None:
ways.remove(now_way)
else:
len_points = len(points)
if len_points == 1:
next_point = points[0]
next_point_x = next_point[0]
next_point_y = next_point[1]
now_way_pasts.append((next_point_x, next_point_y))
elif len_points > 1:
for point in points:
new_way = Way([])
new_way_pasts = new_way.pasts
new_way_pasts.extend(now_way_pasts)
new_way_pasts.append(point)
ways.append(new_way)
ways.remove(now_way)
def fourth_step(points_pixels, points):
pixels = []
for point in points:
pixel = get_pixel(point[0], point[1], points_pixels)
pixels.append(pixel[0])
file = open("maze/maze.zip", "wb")
for element in pixels[1::2]:
file.write(chr(element).encode("latin1"))
print("Done")
def set_logger():
log_file_path = "maze/log/mylog.log"
handler = logging.handlers.RotatingFileHandler(log_file_path, maxBytes=1024 * 1024, backupCount=10000)
fmt = "%(asctime)s=>%(message)s"
formatter = logging.Formatter(fmt)
handler.setFormatter(formatter)
logger = logging.getLogger("maze/log/mylog")
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
def find_next_point(points_pixels, x, y, past, logger):
right_x = x + 1
left_x = x - 1
up_y = y - 1
down_y = y + 1
if right_x > 640:
right_x_pixel = None
else:
right_x_pixel = get_pixel(right_x, y, points_pixels)
if left_x < 0:
left_x_pixel = None
else:
left_x_pixel = get_pixel(left_x, y, points_pixels)
if up_y < 0:
up_y_pixel = None
else:
up_y_pixel = get_pixel(x, up_y, points_pixels)
if down_y > 640:
down_y_pixel = None
else:
down_y_pixel = get_pixel(x, down_y, points_pixels)
points = []
right_point = right_x, y
left_point = left_x, y
up_point = x, up_y
down_point = x, down_y
if right_x_pixel is not None and right_x_pixel != (255, 255, 255, 255):
points.append(right_point)
if left_x_pixel is not None and left_x_pixel != (255, 255, 255, 255):
points.append(left_point)
if up_y_pixel is not None and up_y_pixel != (255, 255, 255, 255):
points.append(up_point)
if down_y_pixel is not None and down_y_pixel != (255, 255, 255, 255):
points.append(down_point)
for point in points:
now_index = past.index((x, y))
before = past[now_index - 1]
if before == point:
points.remove(point)
if len(points) < 1:
return None
return points
def get_pixel(x, y, points_pixels):
return points_pixels["{}_{}".format(x, y)]
if __name__ == "__main__":
main()