|
1 | 1 | fromPILimportImage
|
| 2 | +importlogging |
| 3 | +importlogging.handlers |
| 4 | + |
| 5 | + |
| 6 | +classWay: |
| 7 | +def__init__(self,pasts): |
| 8 | +self.pasts=pasts |
2 | 9 |
|
3 | 10 |
|
4 | 11 | defmain():
|
5 | 12 | file_path="maze/maze.png"
|
6 |
| -first_step(file_path) |
| 13 | +log_file_path="maze/log/mylog.log" |
| 14 | +handler=logging.handlers.RotatingFileHandler(log_file_path,maxBytes=1024*1024,backupCount=10000) |
| 15 | +fmt="%(asctime)s=>%(message)s" |
| 16 | +formatter=logging.Formatter(fmt) |
| 17 | +handler.setFormatter(formatter) |
| 18 | +logger=logging.getLogger("maze/log/mylog") |
| 19 | +logger.addHandler(handler) |
| 20 | +logger.setLevel(logging.DEBUG) |
| 21 | + |
| 22 | +points_pixels,width,height=first_step(file_path) |
| 23 | +# second_step(points_pixels, height) |
| 24 | +start= (639,0) |
| 25 | +third_step(start,points_pixels,logger) |
7 | 26 |
|
8 | 27 |
|
9 | 28 | deffirst_step(file_path):
|
10 | 29 | im=Image.open(file_path)
|
11 | 30 | size=im.size
|
12 | 31 | width=size[0]
|
13 | 32 | height=size[1]
|
| 33 | +points_pixels= {} |
| 34 | +forxinrange(width): |
| 35 | +foryinrange(height): |
| 36 | +points_pixels[("{}_{}".format(x,y))]=im.getpixel((x,y)) |
| 37 | +returnpoints_pixels,width,height |
| 38 | + |
| 39 | + |
| 40 | +defsecond_step(points_pixels,height): |
| 41 | +y=0 |
| 42 | +forxinrange(height): |
| 43 | +pixel=points_pixels["{}_{}".format(x,y)] |
| 44 | +ifpixel!= (255,255,255,255)andpixel!= (127,127,127,255): |
| 45 | +print("x={}, y={}, pixel={}".format(x,y,pixel)) |
| 46 | + |
| 47 | + |
| 48 | +defthird_step(start,points_pixels,logger): |
| 49 | +x=start[0] |
| 50 | +y=start[1] |
| 51 | +pasts= [(x,y)] |
| 52 | +ways= [] |
| 53 | +way=Way(pasts) |
| 54 | +ways.append(way) |
| 55 | +whileTrue: |
| 56 | +iflen(ways)==0: |
| 57 | +break |
| 58 | +else: |
| 59 | +now_way=ways[0] |
| 60 | +now_way_pasts=now_way.pasts |
| 61 | +logger.info("len(ways)={}, now_ways_pasts={}".format(len(ways),now_way_pasts)) |
| 62 | +x=now_way_pasts[-1][0] |
| 63 | +y=now_way_pasts[-1][1] |
| 64 | +points=find_next_point(points_pixels,x,y,now_way_pasts,logger) |
| 65 | +ifpointsisNone: |
| 66 | +# logger.info("removing a way, now_way's pasts={}".format(now_way.pasts)) |
| 67 | +print("removing a way, now_way's pasts={}".format(now_way.pasts)) |
| 68 | +ways.remove(now_way) |
| 69 | +else: |
| 70 | +len_points=len(points) |
| 71 | +iflen_points==1: |
| 72 | +next_point=points[0] |
| 73 | +next_point_x=next_point[0] |
| 74 | +next_point_y=next_point[1] |
| 75 | +ifnext_point_y==640: |
| 76 | +break |
| 77 | +now_way_pasts.append((next_point_x,next_point_y)) |
| 78 | +eliflen_points>1: |
| 79 | +forpointinpoints: |
| 80 | +tmp_pasts=now_way_pasts.copy() |
| 81 | +tmp_pasts.append(point) |
| 82 | +new_way=Way(tmp_pasts) |
| 83 | +ways.append(new_way) |
| 84 | +ways.remove(now_way) |
| 85 | + |
| 86 | + |
| 87 | +deffind_next_point(points_pixels,x,y,past,logger): |
| 88 | +# logger.info("now is ({}, {}) finding next_point".format(x, y)) |
| 89 | +right_x=x+1 |
| 90 | +left_x=x-1 |
| 91 | +up_y=y-1 |
| 92 | +down_y=y+1 |
| 93 | +ifright_x>640: |
| 94 | +right_x_pixel=None |
| 95 | +else: |
| 96 | +right_x_pixel=get_pixel(right_x,y,points_pixels) |
| 97 | +ifleft_x<0: |
| 98 | +left_x_pixel=None |
| 99 | +else: |
| 100 | +left_x_pixel=get_pixel(left_x,y,points_pixels) |
| 101 | +ifup_y<0: |
| 102 | +up_y_pixel=None |
| 103 | +else: |
| 104 | +up_y_pixel=get_pixel(x,up_y,points_pixels) |
| 105 | +ifdown_y>640: |
| 106 | +down_y_pixel=None |
| 107 | +else: |
| 108 | +down_y_pixel=get_pixel(x,down_y,points_pixels) |
| 109 | +points= [] |
| 110 | +right_point=right_x,y |
| 111 | +left_point=left_x,y |
| 112 | +up_point=x,up_y |
| 113 | +down_point=x,down_y |
| 114 | +ifright_x_pixelisnotNoneandright_x_pixel!= (255,255,255,255): |
| 115 | +# logger.info("right_x={}, y={}, right_x_pixel={}".format(right_x, y, right_x_pixel)) |
| 116 | +points.append(right_point) |
| 117 | +ifleft_x_pixelisnotNoneandleft_x_pixel!= (255,255,255,255): |
| 118 | +# logger.info("left_x={}, y={}, left_x_pixel={}".format(left_x, y, left_x_pixel)) |
| 119 | +points.append(left_point) |
| 120 | +ifup_y_pixelisnotNoneandup_y_pixel!= (255,255,255,255): |
| 121 | +# logger.info("x={}, up_y={}, up_y_pixel={}".format(x, up_y, up_y_pixel)) |
| 122 | +points.append(up_point) |
| 123 | +ifdown_y_pixelisnotNoneanddown_y_pixel!= (255,255,255,255): |
| 124 | +# logger.info("x={}, down_y={}, down_y_pixel={}".format(x, down_y, down_y_pixel)) |
| 125 | +points.append(down_point) |
| 126 | +forpointinpoints: |
| 127 | +now_index=past.index((x,y)) |
| 128 | +before=past[now_index-1] |
| 129 | +ifbefore==point: |
| 130 | +# logging.info("removing {}".format(point)) |
| 131 | +points.remove(point) |
| 132 | +iflen(points)<1: |
| 133 | +# logger.info("return None") |
| 134 | +returnNone |
| 135 | +# logger.info("return points={}".format(points)) |
| 136 | +returnpoints |
| 137 | + |
| 138 | + |
| 139 | +defget_pixel(x,y,points_pixels): |
| 140 | +returnpoints_pixels["{}_{}".format(x,y)] |
14 | 141 |
|
15 | 142 |
|
16 | 143 | if__name__=="__main__":
|
|