|
1 | 1 | from PIL import Image
|
| 2 | +import logging |
| 3 | +import logging.handlers |
| 4 | + |
| 5 | + |
| 6 | +class Way: |
| 7 | + def __init__(self, pasts): |
| 8 | + self.pasts = pasts |
2 | 9 |
|
3 | 10 |
|
4 | 11 | def main():
|
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 | def first_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 | + for x in range(width): |
| 35 | + for y in range(height): |
| 36 | + points_pixels[("{}_{}".format(x, y))] = im.getpixel((x, y)) |
| 37 | + return points_pixels, width, height |
| 38 | + |
| 39 | + |
| 40 | +def second_step(points_pixels, height): |
| 41 | + y = 0 |
| 42 | + for x in range(height): |
| 43 | + pixel = points_pixels["{}_{}".format(x, y)] |
| 44 | + if pixel != (255, 255, 255, 255) and pixel != (127, 127, 127, 255): |
| 45 | + print("x={}, y={}, pixel={}".format(x, y, pixel)) |
| 46 | + |
| 47 | + |
| 48 | +def third_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 | + while True: |
| 56 | + if len(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 | + if points is None: |
| 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 | + if len_points == 1: |
| 72 | + next_point = points[0] |
| 73 | + next_point_x = next_point[0] |
| 74 | + next_point_y = next_point[1] |
| 75 | + if next_point_y == 640: |
| 76 | + break |
| 77 | + now_way_pasts.append((next_point_x, next_point_y)) |
| 78 | + elif len_points > 1: |
| 79 | + for point in points: |
| 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 | +def find_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 | + if right_x > 640: |
| 94 | + right_x_pixel = None |
| 95 | + else: |
| 96 | + right_x_pixel = get_pixel(right_x, y, points_pixels) |
| 97 | + if left_x < 0: |
| 98 | + left_x_pixel = None |
| 99 | + else: |
| 100 | + left_x_pixel = get_pixel(left_x, y, points_pixels) |
| 101 | + if up_y < 0: |
| 102 | + up_y_pixel = None |
| 103 | + else: |
| 104 | + up_y_pixel = get_pixel(x, up_y, points_pixels) |
| 105 | + if down_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 | + if right_x_pixel is not None and right_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 | + if left_x_pixel is not None and left_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 | + if up_y_pixel is not None and up_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 | + if down_y_pixel is not None and down_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 | + for point in points: |
| 127 | + now_index = past.index((x, y)) |
| 128 | + before = past[now_index - 1] |
| 129 | + if before == point: |
| 130 | + # logging.info("removing {}".format(point)) |
| 131 | + points.remove(point) |
| 132 | + if len(points) < 1: |
| 133 | + # logger.info("return None") |
| 134 | + return None |
| 135 | + # logger.info("return points={}".format(points)) |
| 136 | + return points |
| 137 | + |
| 138 | + |
| 139 | +def get_pixel(x, y, points_pixels): |
| 140 | + return points_pixels["{}_{}".format(x, y)] |
14 | 141 |
|
15 | 142 |
|
16 | 143 | if __name__ == "__main__":
|
|
0 commit comments