|
| 1 | +from PIL import Image |
| 2 | +import logging |
| 3 | + |
| 4 | + |
| 5 | +def main(): |
| 6 | + logging.basicConfig(filename="maze/24.log", filemode="w", level=logging.DEBUG, |
| 7 | + format='%(asctime)s=>%(message)s') |
| 8 | + file_path = "maze/maze.png" |
| 9 | + im = first_step(file_path) |
| 10 | + im_size = im.size |
| 11 | + width = im_size[0] # 641 |
| 12 | + height = im_size[1] # 641 |
| 13 | + # second_step(im, height) |
| 14 | + start = (639, 0) |
| 15 | + third_step(im, start, width, height) |
| 16 | + |
| 17 | + |
| 18 | +def first_step(file_path): |
| 19 | + im = Image.open(file_path) |
| 20 | + return im |
| 21 | + |
| 22 | + |
| 23 | +def second_step(im, height): |
| 24 | + y = 0 |
| 25 | + for x in range(height): |
| 26 | + pixel = im.getpixel((x, y)) |
| 27 | + if pixel != (255, 255, 255, 255) and pixel != (127, 127, 127, 255): |
| 28 | + print("x={}, y={}, pixel={}".format(x, y, pixel)) |
| 29 | + |
| 30 | + |
| 31 | +def third_step(im, start, width, height): |
| 32 | + x = start[0] |
| 33 | + y = start[1] |
| 34 | + point_ary = [(x, y)] |
| 35 | + way = "0" |
| 36 | + ways = {way: point_ary} |
| 37 | + go_to_end(im, x, y, point_ary, way, ways) |
| 38 | + |
| 39 | + |
| 40 | +def router(im, points, way, ways): |
| 41 | + logging.info("router start: ways={}".format(ways)) |
| 42 | + tmp_way_value = ways[way] |
| 43 | + logging.info("delting way={}".format(way)) |
| 44 | + del ways[way] |
| 45 | + for i in range(len(points)): |
| 46 | + point = points[i] |
| 47 | + next_way = str(int(way) + 1 + i) |
| 48 | + logging.info("next_way={}".format(next_way)) |
| 49 | + ways[next_way] = tmp_way_value |
| 50 | + x = point[0] |
| 51 | + y = point[1] |
| 52 | + next_point_ary = ways[next_way] |
| 53 | + next_point_ary.append((x, y)) |
| 54 | + tmp_ways = ways |
| 55 | + tmp_ways_keys = tmp_ways.keys() |
| 56 | + for key in tmp_ways_keys: |
| 57 | + now_points = tmp_ways[key] |
| 58 | + now_points_tail = now_points[-1] |
| 59 | + x = now_points_tail[0] |
| 60 | + y = now_points_tail[1] |
| 61 | + go_to_end(im, x, y, now_points, key, ways) |
| 62 | + logging.info("router end: ways={}".format(ways)) |
| 63 | + |
| 64 | + |
| 65 | +def go_to_end(im, x, y, point_ary, way, ways): |
| 66 | + logging.info("go_to_end start: ways={}".format(ways)) |
| 67 | + points = find_next_point(im, x, y, point_ary) |
| 68 | + if points is None: |
| 69 | + logging.info("delting way={}".format(way)) |
| 70 | + del ways[way] |
| 71 | + else: |
| 72 | + len_points = len(points) |
| 73 | + if len_points == 1: |
| 74 | + x = points[0][0] |
| 75 | + y = points[0][1] |
| 76 | + point_ary.append((x, y)) |
| 77 | + go_to_end(im, x, y, point_ary, way, ways) |
| 78 | + else: |
| 79 | + router(im, points, way, ways) |
| 80 | + |
| 81 | + |
| 82 | +def find_next_point(im, x, y, ok_points): |
| 83 | + logging.info("now is ({}, {}) finding next_point".format(x, y)) |
| 84 | + right_x = x + 1 |
| 85 | + left_x = x - 1 |
| 86 | + up_y = y - 1 |
| 87 | + down_y = y + 1 |
| 88 | + if right_x > 640: |
| 89 | + right_x_pixel = None |
| 90 | + else: |
| 91 | + right_x_pixel = im.getpixel((right_x, y)) |
| 92 | + if left_x < 0: |
| 93 | + left_x_pixel = None |
| 94 | + else: |
| 95 | + left_x_pixel = im.getpixel((left_x, y)) |
| 96 | + if up_y < 0: |
| 97 | + up_y_pixel = None |
| 98 | + else: |
| 99 | + up_y_pixel = im.getpixel((x, up_y)) |
| 100 | + if down_y > 640: |
| 101 | + down_y_pixel = None |
| 102 | + else: |
| 103 | + down_y_pixel = im.getpixel((x, down_y)) |
| 104 | + points = [] |
| 105 | + right_point = right_x, y |
| 106 | + left_point = left_x, y |
| 107 | + up_point = x, up_y |
| 108 | + down_point = x, down_y |
| 109 | + next_director = {} |
| 110 | + if right_x_pixel is not None and right_x_pixel != (255, 255, 255, 255): |
| 111 | + # logging.info("right_x={}, y={}, right_x_pixel={}".format(right_x, y, right_x_pixel)) |
| 112 | + points.append(right_point) |
| 113 | + if left_x_pixel is not None and left_x_pixel != (255, 255, 255, 255): |
| 114 | + # logging.info("left_x={}, y={}, left_x_pixel={}".format(left_x, y, left_x_pixel)) |
| 115 | + points.append(left_point) |
| 116 | + if up_y_pixel is not None and up_y_pixel != (255, 255, 255, 255): |
| 117 | + # logging.info("x={}, up_y={}, up_y_pixel={}".format(x, up_y, up_y_pixel)) |
| 118 | + points.append(up_point) |
| 119 | + if down_y_pixel is not None and down_y_pixel != (255, 255, 255, 255): |
| 120 | + # logging.info("x={}, down_y={}, down_y_pixel={}".format(x, down_y, down_y_pixel)) |
| 121 | + points.append(down_point) |
| 122 | + for point in points: |
| 123 | + now_index = ok_points.index((x, y)) |
| 124 | + before = ok_points[now_index - 1] |
| 125 | + if before == point: |
| 126 | + logging.info("removing {}".format(point)) |
| 127 | + points.remove(point) |
| 128 | + if len(points) < 1: |
| 129 | + logging.info("return None") |
| 130 | + return None |
| 131 | + logging.info("return points={}".format(points)) |
| 132 | + points2 = [points[0]] |
| 133 | + return points2 |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + main() |
0 commit comments