1
- from PIL import Image , ImageSequence
1
+ from PIL import Image
2
+ import logging
2
3
3
4
4
5
def main ():
6
+ logging .basicConfig (filename = "maze/24.log" , filemode = "w" , level = logging .DEBUG ,
7
+ format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s' )
5
8
file_path = "maze/maze.png"
6
9
im = first_step (file_path )
7
10
im_size = im .size
8
11
width = im_size [0 ] # 641
9
12
height = im_size [1 ] # 641
10
- second_step (im , height )
13
+ # second_step(im, height)
11
14
start = (639 , 0 )
12
15
third_step (im , start , width , height )
13
16
@@ -28,30 +31,43 @@ def second_step(im, height):
28
31
def third_step (im , start , width , height ):
29
32
x = start [0 ]
30
33
y = start [1 ]
34
+ way = "0"
35
+ max_key = int (way )
31
36
ok_points = [(x , y )]
32
- one_way (im , start , x , y , ok_points )
37
+ ok_points_dic = {way : ok_points }
38
+ one_way (im , x , y , ok_points_dic , max_key )
33
39
34
40
35
- def one_way (im , start , x , y , ok_points ):
41
+ def one_way (im , x , y , ok_points_dic , max_key ):
36
42
while True :
37
- print ("ok_points={}" .format (ok_points ))
38
- next_points = find_next_point (im , x , y , ok_points )
39
- print ("point=({}, {}),next_point={}" .format (x , y , next_points ))
40
- if next_points is None :
41
- print ("break code 1" )
42
- break
43
- else :
44
- if len (next_points ) == 1 :
45
- x = next_points [0 ][0 ]
46
- y = next_points [0 ][1 ]
47
- ok_points .append ((x , y ))
48
- else :
49
- print ("len(next_points)={}" .format (len (next_points )))
50
- print ("break code 2" )
43
+ logging .debug ("ok_points_dic={}" .format (ok_points_dic ))
44
+ keys = ok_points_dic .keys ()
45
+ to_be_delete = set ()
46
+ to_be_add_dict = dict ()
47
+ for way in keys :
48
+ way_ok_points = ok_points_dic .get (way )
49
+ next_points = find_next_point (im , x , y , way_ok_points , way )
50
+ if next_points is None :
51
+ logging .debug ("break code 1" )
52
+ to_be_delete .add (way )
53
+ logging .debug ("add way={} to be delete, because next_points is None" .format (way ))
51
54
break
55
+ else :
56
+ for point in next_points :
57
+ max_key += 1
58
+ to_be_delete .add (way )
59
+ logging .debug ("add way={}to to be delete, because create a new one={}" .format (way , max_key ))
60
+ to_be_add_dict [str (max_key )] = way_ok_points
61
+ x = point [0 ]
62
+ y = point [1 ]
63
+ way_ok_points .append ((x , y ))
64
+ for element in to_be_delete :
65
+ del ok_points_dic [element ]
66
+ for key in to_be_add_dict .keys ():
67
+ ok_points_dic [key ] = to_be_add_dict [key ]
52
68
53
69
54
- def find_next_point (im , x , y , ok_points ):
70
+ def find_next_point (im , x , y , ok_points , way ):
55
71
right_x = x + 1
56
72
left_x = x - 1
57
73
up_y = y - 1
@@ -89,13 +105,14 @@ def find_next_point(im, x, y, ok_points):
89
105
if down_y_pixel is not None and down_y_pixel != (255 , 255 , 255 , 255 ):
90
106
print ("x={}, down_y={}, down_y_pixel={}" .format (x , down_y , down_y_pixel ))
91
107
points .append (down_point )
92
- print ( " right_x_pixel={}, left_x_pixel={}, up_y_pixel={}, down_y_pixel={}" .format (right_x_pixel , left_x_pixel ,
108
+ logging . debug ( "way={}, x={}, y={}, right_x_pixel={}, left_x_pixel={}, up_y_pixel={}, down_y_pixel={}" .format (way , x , y , right_x_pixel , left_x_pixel ,
93
109
up_y_pixel , down_y_pixel ))
94
110
for point in points :
95
111
if point in ok_points :
112
+ logging .debug ("way={}, remove point={}" .format (way , point ))
96
113
points .remove (point )
97
114
if len (points ) < 1 :
98
- print ("points为None,x={}, y={}" .format (x , y ))
115
+ # print("points为None,x={}, y={}".format(x, y))
99
116
return None
100
117
return points
101
118
0 commit comments