Maze creation in Python

Hello community,

I am very new to this platform/software. I am simply trying to make a maze that goes over a text image.

I got a code from an AI source but it is not computing with Python. Please tell me what I am missing.

Here is the code I was using. I have gotten Sytax error, indentation error, then a sea of red.

create_maze_with_text(image_path, complexity=10, density=10):

Load the image and convert it to grayscale to use as a mask

image = Image.open(image_path).convert(“L”)
width, height = image.size
maze = np.zeros((height, width), dtype=np.uint8)

Add borders to the maze

maze[0, :] = 1
maze[-1, :] = 1
maze[:, 0] = 1
maze[:, -1] = 1

Add internal walls based on complexity and density

for _ in range(complexity * (width + height)):
x, y = np.random.randint(0, width), np.random.randint(0, height)
if image.getpixel((x, y)) > 200: # Only place walls within text areas (white areas)
maze[y, x] = 1

Create a path through the maze

start_x, start_y = 1, 1
end_x, end_y = width - 2, height - 2
x, y = start_x, start_y

while x != end_x or y != end_y:
maze[y, x] = 0 # Mark the path
neighbors =
if x > 1: neighbors.append((x - 1, y))
if x < width - 2: neighbors.append((x + 1, y))
if y > 1: neighbors.append((x, y - 1))
if y < height - 2: neighbors.append((x, y + 1))
x, y = neighbors[np.random.randint(len(neighbors))]

Ensure start and end points are open

maze[start_y, start_x] = 0
maze[end_y, end_x] = 0

Draw the maze over the original image

maze_image = Image.fromarray(np.uint8(maze * 255))
result = Image.composite(Image.new(“L”, (width, height), 0), image, maze_image)
return result

Paths

input_image_path = “C:\Users\mdoug\Downloads\Recover the truth.png”
output_image_path = “generated_maze.png”

Resize the input image for faster processing

image = Image.open(input_image_path)
resized_image = image.resize((500, 500)) # Reduce to 500x500
resized_image.save(“resized_image.png”)

Generate the maze

maze_image = create_maze_with_text(“resized_image.png”, complexity=15, density=15)
maze_image.save(output_image_path)

print(f"Maze saved as {output_image_path}")

That’s because AIs are pretty terrible at writing code. Throw it out and start from scratch. Define your problem, start figuring out how to solve it.

There are generalized maze generation algorithms out there, so it’s mostly going to be up to you to define what it means for the maze to “go over” the text image.

5 Likes

Honestly I think copilot and perplexity.ai are pretty good at writing Python.

But you need to know python to use their code well. I’d recommend solving some puzzles on exercism.org (or a similar repository) before returning to this problem. You really need to be able to resolve indentation errors yourself, and it’s not hard to learn if you start small.

Thank you for this feedback!

Thank you for the resource!!
The link you shared leads to some weird page tho.

Thanks for pointing that out. It should have been

(I think I don’t have enough trust level to correct my original post (?))

Help us help you. Read this link first and learn how to format code so we can help you better. Do not use a screen shot of your code as some of us will copy and paste the code to get it to work for you, so you can learn from this. About the Python Help category You will get more help this way.

Yes, I’ve found the Gemini AI is terrible at writing whole programs, so I just ask it to write sections of code, or just ask it how to do one step, and even then it often (about 50% of the time) uses outdated modules, or something else doesn’t work. That’s why I use a search engine to answer my programming questions because I can limit websites to the past year, and that works much better.

However there are VSC extensions that can write code for you. At least AICodeKing on Youtube says they are great but he only does really simple examples. He is reviewing VSC AI extensions almost daily.

Thank you for these resources and food for thought about filtering my search!
This has been an interesting journey :slight_smile: