|
| 1 | +from PIL import Image, ImageSequence |
| 2 | + |
| 3 | + |
| 4 | +def main(): |
| 5 | + file_path = "maze/maze.png" |
| 6 | + im = first_step(file_path) |
| 7 | + im_size = im.size |
| 8 | + width = im_size[0] # 641 |
| 9 | + height = im_size[1] # 641 |
| 10 | + second_step(im, height) |
| 11 | + start = (639, 0) |
| 12 | + third_step(im, start, width, height) |
| 13 | + |
| 14 | + |
| 15 | +def first_step(file_path): |
| 16 | + im = Image.open(file_path) |
| 17 | + return im |
| 18 | + |
| 19 | + |
| 20 | +def second_step(im, height): |
| 21 | + y = 0 |
| 22 | + for x in range(height): |
| 23 | + pixel = im.getpixel((x, y)) |
| 24 | + if pixel != (255, 255, 255, 255) and pixel != (127, 127, 127, 255): |
| 25 | + print("x={}, y={}, pixel={}".format(x, y, pixel)) |
| 26 | + |
| 27 | + |
| 28 | +def third_step(im, start, width, height): |
| 29 | + x = start[0] |
| 30 | + y = start[1] |
| 31 | + ok_points = [(x, y)] |
| 32 | + one_way(im, start, x, y, 0, ok_points) |
| 33 | + |
| 34 | + |
| 35 | +def one_way(im, start, x, y, key, ok_points): |
| 36 | + while True: |
| 37 | + print("key={}, ok_points={}".format(key, ok_points)) |
| 38 | + next_points = find_next_point(im, x, y, ok_points) |
| 39 | + print("key={}, point=({}, {}),next_point={}".format(key, 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 | + for point in next_points: |
| 50 | + key += 1 |
| 51 | + x = point[0] |
| 52 | + y = point[1] |
| 53 | + one_way(im, start, x, y, key, ok_points) |
| 54 | + |
| 55 | + |
| 56 | +def find_next_point(im, x, y, ok_points): |
| 57 | + right_x = x + 1 |
| 58 | + left_x = x - 1 |
| 59 | + up_y = y - 1 |
| 60 | + down_y = y + 1 |
| 61 | + if right_x > 640: |
| 62 | + right_x_pixel = None |
| 63 | + else: |
| 64 | + right_x_pixel = im.getpixel((right_x, y)) |
| 65 | + if left_x < 0: |
| 66 | + left_x_pixel = None |
| 67 | + else: |
| 68 | + left_x_pixel = im.getpixel((left_x, y)) |
| 69 | + if up_y < 0: |
| 70 | + up_y_pixel = None |
| 71 | + else: |
| 72 | + up_y_pixel = im.getpixel((x, up_y)) |
| 73 | + if down_y > 640: |
| 74 | + down_y_pixel = None |
| 75 | + else: |
| 76 | + down_y_pixel = im.getpixel((x, down_y)) |
| 77 | + points = [] |
| 78 | + right_point = right_x, y |
| 79 | + left_point = left_x, y |
| 80 | + up_point = x, up_y |
| 81 | + down_point = x, down_y |
| 82 | + if right_x_pixel is not None and right_x_pixel != (255, 255, 255, 255): |
| 83 | + print("right_x={}, y={}, right_x_pixel={}".format(right_x, y, right_x_pixel)) |
| 84 | + points.append(right_point) |
| 85 | + if left_x_pixel is not None and left_x_pixel != (255, 255, 255, 255): |
| 86 | + print("left_x={}, y={}, left_x_pixel={}".format(left_x, y, left_x_pixel)) |
| 87 | + points.append(left_point) |
| 88 | + if up_y_pixel is not None and up_y_pixel != (255, 255, 255, 255): |
| 89 | + print("x={}, up_y={}, up_y_pixel={}".format(x, up_y, up_y_pixel)) |
| 90 | + points.append(up_point) |
| 91 | + if down_y_pixel is not None and down_y_pixel != (255, 255, 255, 255): |
| 92 | + print("x={}, down_y={}, down_y_pixel={}".format(x, down_y, down_y_pixel)) |
| 93 | + points.append(down_point) |
| 94 | + print("right_x_pixel={}, left_x_pixel={}, up_y_pixel={}, down_y_pixel={}".format(right_x_pixel, left_x_pixel, |
| 95 | + up_y_pixel, down_y_pixel)) |
| 96 | + for point in points: |
| 97 | + if point in ok_points: |
| 98 | + points.remove(point) |
| 99 | + if len(points) < 1: |
| 100 | + print("points为None,x={}, y={}".format(x, y)) |
| 101 | + return None |
| 102 | + return points |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments