Skip to content

How to solve a maze with Photoshop

wow, this is cool. lod3n, confronted by this heinous puzzle, wrote:

'2 minutes in Photoshop. All too easy. So, where do I pick up my cake?

  1. Increase contrast.
  2. Select the right wall of the maze using the magic wand.
  3. Select > Modify > Expand 4 pixels
  4. Create new layer.
  5. Fill with Red.
  6. Select > Modify > Contract 2 pixels.
  7. Delete. Now you've got a line tracing the solution.
  8. Manually clean up the outer edge, and connect the dots.
  9. Cake!'

Here's the result. Seriously nifty!

(Update: wow, this got Dugg heavily -- 17000 pageviews from Digg alone! Unfortunately that caused a bit of a server meltdown. Should be back now though...)

hacks mazes pathfinding photoshop

118 Comments

  1. June 19, 2007

    That's just taking all the fun out of it!

  2. June 20, 2007

    This is brilliant!!

  3. June 20, 2007

    Anyone know how to do this with the GIMP?

  4. June 20, 2007

    The same way?

  5. June 20, 2007

    yep, I did it the same way using The Gimp -- the only difference is that, instead of "Select -> Modify -> Expand / Contract", you have "Select -> Grow / Shrink".

    (ps: woo! this post made Hotlinks, Reddit and Waxy's links!)

  6. Androse Rosewood
    June 20, 2007

    Nice trick :)

    This uses the brute force technique to solving any maze: follow one side of the wall (right or left, it doesn't matter), and you'll end up at the exit.

  7. June 20, 2007

    This must be the most humorous and completely unexpected use of PhotoShop I've ever seen.

    I really should remember this technique, who knows, it may come in handy one day.

  8. June 20, 2007

    if, as Androse points out, this is just using the left-hand rule, it will fail for complex mazes with loops. Of course, most mazes don't seem to have loops in them. And either way, it's a nice hack.

  9. Bruce
    June 20, 2007

    Waider, is there any examples of mazes with loops that this system wouldn't be able to solve? Even a simplified diagram?

    I can't work out how that would look...

    btw. absolutely brilliant idea, cheers for posting it. Makes me wonder how machines might see one day... filling in patterns in bizarre new ways we wouldn't expect.

  10. Anonymous
    June 20, 2007

    This is delicious cake!

  11. dc
    June 20, 2007

    Bruce,

    maze would have to have a start or end withing itself rather then on an outer edge and a loop could throw the solve by following a wall system off.

  12. June 20, 2007

    Seems like any maze with a loop would also allow you to shortcut the loop (that is, the loop would be redundant, because you could just cut the corner of it and carry on)

    Great idea - can't wait to see how else this could apply.

  13. Penh
    June 20, 2007

    Any maze in which you can draw a closed path that separates the start from the goal can't be solved with the left- or right-hand rule. It doesn't apply to mazes like this one, but if the goal is within the maze and you can draw a path that completely encloses it, the hand rule won't be able to get you past that loop, since there's no wall to follow across it.

  14. June 20, 2007

    I was able to skip step 6 by changing step 5 to "Create 1px Stroke around selection."

  15. June 21, 2007

    OMGHAX!

  16. MauricioC
    June 21, 2007

    Interestingly enough, a way to solve these kinds of mazes algorithmically is using Depth-First Search, which is one of the ways a Flood fill algorithm (i.e., the algorithm Paint/Photoshop/Any other graphics tool uses to paint or find regions) can be done. So, basically, painting a region and solving a maze is the same thing, from this point of view.

    Very cool.

  17. Paulo
    June 21, 2007

    It's easier if you just use expand 4 pixels and then stroke the selection. (click with right mouse button and select stroke). 2 or 3 pixels width and center option makes it look nice with fewer steps. There is no need to create a new layer either.

  18. Richard
    June 21, 2007

    There's an easier way..... after you've done the "expand", change tool to "Rectangular Marquee" and stroke with red, 2px, center. Voila! Avoids all the layering/fill/delete guff. All that's left is the tidy up job.

  19. Richard
    June 21, 2007

    Ahh dammit, I was beaten to it.

  20. June 21, 2007

    rofl, very nice

  21. Jim
    June 21, 2007

    Increase contrast by how much? What tolerance settings for the magic wand? Some details are left out of the directions.

  22. Scribbleed
    June 21, 2007

    Other than combining steps 5-7 with a selection stroke, you should then

    (a) Select the big empty external space (in the above maze, the lower left area),
    (b) Pick another color, e.g. Blue,
    (c) Stroke the selection with 1 px size, either on a new layer or back on the original,

    And you'd have a line tracing the solution. Don't even have to tidy, clean up, etc.

  23. DaedriX
    June 21, 2007

    Increase contrast to the maximum, make it Black and White. And then for tolerance settings it won't really matter.

  24. Tom
    June 21, 2007

    This is so much more complicated than it needs to be. Find the exit and do a continuous magic wand. Fill. BAM! There, works every time for a high enough contrast image.

  25. June 21, 2007

    [this is good]

  26. June 21, 2007

    ZOMG HAXORS!! Wow, I didn't see that coming. Nice use of photoshop.

  27. mauszozo
    June 21, 2007

    Here's a maze type it doesn't work on:

    http://www.glassgiant.com/maze/

    The start and end points are inside the maze, and all walls are connected.

  28. June 21, 2007

    Wow that's an insane way to solve the problem.

  29. Tom
    June 22, 2007

    Can you do this with Paint.NET?

  30. June 22, 2007

    Seriously cool the things that you can find on Digg. Where was this when I was programatically solving mazes as CS projects? Not that it would have helped, but it would have at least been a welcome distraction from the tedious code that I never did get to work quite correctly...

  31. Tom
    June 23, 2007

    Brilliant idea. Thanks for very interesting post. btw. I really enjoyed reading all of your posts. It's interesting to read ideas, and observations from someone else's point of view… makes you think more.

  32. Jack
    June 24, 2007

    i dont get this tutorial. What do you mean when you say: Select the right wall of the maze using the magic wand.? Do you select the right edge of the wall? I tried with maze in the tutorial but selecting the edge takes a while and it also selects the inner walls.

  33. DaedriX
    June 24, 2007

    Yea, you just select the right wall. (as if you were walking into the maze) And then the tutorial should work fine.

  34. Jason
    June 24, 2007

    Save as 2 color bitmap in paint.

    Save as 24 bit bitmap in paint.

    Fill one wall with light color.

    Maze solved in paint.

  35. RobM
    June 25, 2007

    You know... or you could:
    1) Image > Adjustments > Black & White
    2) Color one of the side walls blue.

    There is your path.

  36. June 25, 2007

    everyone who says to paint one wall: that will give you a solution. but it will give you a path that, in the worst case, will have to travel down every single dead end on the maze. this method will give you a better solution. if you notice, it doesn't follow any dead ends. it gives an optimal left- or right-hand path from start to finish. if the maze only has one unique solution, it will give you the optimal path through the maze.

  37. June 28, 2007

    Interesting - Step 2-5 basically select the solid part of the maze which has a clear outer edge which is the valid path - instead of selecting the 'right wall', you can try to select the left wall, and get similar result.

    Very interesting - Thank you.

  38. June 28, 2007

    For Mazes like this, with no loops and with the entrance and exit on the outer wall, the Maze is effectively split in two by the solution path. The left half section of walls and right half section of walls are each like a tree, with the solution path going between them.

    Therefore, an even simpler way to solve a Maze of this type in an image editing program is to flood fill one of the walls with a different color. That won't show the solution as a line, however it does allow you to navigate the Maze by the most direct route without error, if you simply ensure you're always in a passage between walls of different colors.

    The original technique posted works similarly to this, except it thickens one of the large wall sections, until all the internal passages are squeezed into nothingness, leaving just a large solid block shape. The inside edge of that shape goes down the solution path (because it's the boundary between it and the other wall section) where highlighting that inner edge shows the direct solution.

    For additional methods solving Mazes, such as Depth First Search and other algorithms, see http://www.astrolog.org/labyrnth/algrithm.htm#solve

  39. lildevil
    June 28, 2007

    I tried this with a different extremely complex maze and it works.

    As long as there is one entry way and one exit, it should work.

  40. Doug
    June 28, 2007

    The solution at the top is too complicated and can be simplified to 2 steps.

    Increase contrast of maze with Image > Adjustments > Brightness/Contrast > Contrast slider to right for maximum contrast.

    Select paint bucket tool with contrasting color (to black maze) such as primary red. Using paint bucket tool click on wall under the word You at top of maze. Right 1/2 of maze's walls turn into color.

    Solution is gap between colors. Cake!

  41. john
    June 28, 2007

    Alright - call me the village idiot - but when I follow the instructions, exanding the magic-wand selection by 4 pixels causes the WHOLE image to be selected. What am I missing? Any advice? Thanks!

  42. Sand
    June 28, 2007

    there's an even simpler solution. go around the maze.

  43. June 29, 2007

    i'll have to remember next time i'm caught in a maze, with a map , and a copy of photoshop.

    damn it where the hell am I.

  44. June 29, 2007

    first rule!

  45. July 1, 2007

    hey lod3n! Lucy and Mario Dinis say hi ;)

  46. July 13, 2007

    I can't make this work, no matter whose instructions I use... What in the hell am I doing wrong?? By "right wall" do you mean right black line of the maze, or the white space to the right of the maze?? And what layer am I supposed to be on when I am doing each step?? You're simplifying the instructions to make it sound like it's easy as pie. Please expand your instructions. I wanna know what the tolerance of my selection tool should be at and everything! I use Photoshop a lot and my settings are probably different than yours. GRRR, frustration!

  47. July 16, 2007

    Here are some expanded instructions:

    1. Increase the contrast of the maze until it looks like a black and white maze.
    2. Select the right wall (the black part - you can select any black line you see). of the maze using the magic wand. Your tolerance should be set to something like 32, no anti-aliasing. The reason this works, is that any maze where the entrance and exit are on the outside of the maze via doors in the maze has only two walls. These walls are intertwined, but there are still only two of them. When this step is complete, you should have a shimmering selection set on about 50% of the maze.
    3. From the menu Select > Modify > Expand 4 pixels. This makes the selected area slightly larger than the wall.
    4. Create new layer, and name the layer "Solution". Switch to the Solution layer.
    5. Maintaining the Selection from step 3, Fill the Solution layer with Red.
    6. Select > Modify > Contract 2 pixels. This shrinks the selected area, so that in the next step, we can delete everything except the solution.
    7. Press Delete, and select nothing.. Now you've got a line tracing the solution on the layer called Solution.
    8. Manually clean up the outer edge with the Eraser tool, and connect the dots with the Pencil tool.
    9. Cake!'

    Hope this helps.

  48. Jack Wraggs
    July 24, 2007

    Who is 'lod3n'? And where was this originally published?

  49. July 24, 2007

    lod3n is the guy who just posted a comment above yours. I can't tell you where it was originally published.

  50. September 17, 2007

    Cool, a $750 answer to a maze. This must be one of the most unique ways to use photoshop I've seen!

  51. October 31, 2007

    I was able to skip step 6 by changing step 5 to "Create 1px Stroke around selection".

  52. November 20, 2007

    Haha, so obvious yet so genius :)

  53. Roy
    January 6, 2008

    I decided to give it a try, and checked it on another maze - and it really worked - AMAZING !!!

  54. February 8, 2008

    Such a simple solution but it worked like a charm - well done.
    Thanks for the info

  55. February 15, 2008

    Thank you it's simple but work at 100%. I noob in Photoshop but hope that this sample help me increase expirience.

  56. March 11, 2008

    You guys must have a LOTof time!! LOL I love this stuff though. I find it in Stumble Upon, think it's cool, and add it to my links2.html page! Simply amazing to me that there are people so deeply involved in so many application areas of computing. Thanks!

  57. Feoremar
    March 25, 2008

    The cake is a lie

  58. Blanka
    June 11, 2008

    Just make it a PNG file. Then it is 68 Kb instead of 1.5 meg! Prevents a meltdown next time.

  59. Brett
    June 11, 2008

    The expanded instructions helped ALOT, thanks. absolutly amazing!!!

  60. June 12, 2008

    @Blanka: I presume you tried that? I must get a copy of your PNG compression software, as mine produces a 7MB file -- much larger than the 1.5MB jpeg...

  61. Jake
    June 12, 2008

    Are you guys seriously going to make me say it? Ugh, fine.

    A-MAZE-ING!

  62. June 12, 2008

    doh, you're right! sorry Blanka.

    I can't get 56.4KB out of The Gimp, but even the 660KB it gives me by switching to a small number of indexed colours in the palette makes a difference.

  63. June 12, 2008

    Feel free to snag the copy I posted, if it will help your bandwidth. :)

  64. July 11, 2008

    I just love this, the maze solving Photoshop tip and the debate in the comments, nice. Believe it or not, I jumped through couple of blogs until I reached here and being able to actually read the mini-tutorial.

  65. August 2, 2008

    Oh my god this is genius.

  66. Perry Moluman
    August 15, 2008

    The idea of solving a maze is to do it on your own and actually think. Using photoshop to solve a maze is like taking the stickers off of a rubix cube and putting the colors together.

  67. August 15, 2008

    Propecia,

    You make an excellent point, which I'm quite sure you're ready to demonstrate!

    I'm not the smartest guy in the world, so solving this maze manually seemed nearly impossible in any reasonable amount of time, which is why my own lower-powered brain downshifted into "cheat at image manipulation with Photoshop" mode.

    Please, show us how it's done properly, and solve this maze on your own with actual thinking, and send us your incredible proof of the superior maze-solving methodology. It would be interesting to know exactly how many minutes or seconds it takes you to reach the end, so a quick video of the process would be the preferable format.

    Here is your maze:
    http://www.saneasylum.com/images/PropeciaMaze.png

    Ready? Set... GO!

  68. September 16, 2008

    For me, the essential missing setting, even from lod3n's expanded instructions, was to have the wand set on "contiguous" ...otherwise, it would just select all black pixels on the screen, which was monumentally frustrating. :)

  69. September 16, 2008

    (P.S. lod3n: That's one hell of a maze you set for Propecia! :D)

    (P.P.S. I know you're being sarcastic, but even if this is a "cheat" solution to mazes of this type, you still had to know something about mazes (i.e. that they only have two walls) before you could jump to the solution. I didn't know that, so I thank you for teaching me something new.)

  70. October 9, 2008

    very call and of course the subject is photoshop, my favorite program

  71. Panarchy
    October 15, 2008

    Wowie

    Gotta try this!

    Awesome idea... though how'd you think of it?

  72. November 1, 2008

    So simple - and yet it can solve any maze. unbelieveable !!!

  73. December 13, 2008

    Cool - I love this!
    Solving the maze with Photoshop - now we need to find the method to solve Soduko :-)

  74. December 14, 2008

    Absolutly Brilliant idea !

  75. December 29, 2008

    ? have followed your writing for a long time.really you have given very successful information.

  76. January 11, 2009

    it would just select all black pixels on the screen, which was monumentally frustrating.

  77. January 31, 2009

    very call and of course the subject is photoshop, my favorite program

  78. DaddyLongLegs
    February 14, 2009

    I got a simpler way. try this.
    (excuse my poor english, 'cause I'm Korean and english is not that good.)
    This doesn't work with a maze which has loops or exit in the middle of it.

    1. paint the left wall of the entrance with red(RGB 255.0.0). *** anti-alias option must be checked.
    2. paint the right wall of the entrance with blue (RGB 0.0.255) *** anti-alias option must be checked.
    3. select the way using a magic wand tool. (tolerance 0. anti-alias unchecked) The color of the path is RGB .223. 195. 227.
    4. fill it with your favorite color.
  79. DaddyLongLegs
    February 14, 2009

    I forgot something.
    My way works only with a maze which has 1 pixel-wide wall and path.
    Before using the method, maze need to be adjust with some filters (maximum or minimum) and to be resized.

  80. February 15, 2009

    You know how you tell yourself you are studying because you have your certification books opened in front of you? But you are really clicking on Stumble Upon to find interesting posts to read?

    Yeah well, I came across yours and had to write to tell you I enjoyed it very much. I gave it the thumbs up, so more people can come across it and enjoy it also.

  81. February 25, 2009

    Wow, what's the point of solving it this way? Takes all the fun out of it if you ask me.

  82. July 20, 2009

    You know how you tell yourself you are studying because you have your certification books opened in front of you? But you are really clicking on Stumble Upon to find interesting posts to read?

    Yeah well, I came across yours and had to write to tell you I enjoyed it very much. I gave it the thumbs up, so more people can come across it and enjoy it also.

  83. August 3, 2009

    Thanks, very call and of course the subject is photoshop, my favorite program

  84. August 18, 2009

    Good thinking! Thanks for the post!

  85. October 7, 2009

    I read this post a couple of months ago, and 2 weeks ago I had a question in one of my university classes, asking "try to build a game using a graphic software", It took me couple of minutes to remeber, but when i suggested this solution as an option, my lecturer was amazed of the solution I offered :-)

    Thanks :-)

  86. November 8, 2009

    I'm really very useful to follow a long-time see this as a blog here Thank you for your valuable information.

  87. frede
    December 8, 2009

    Ha! I was looking for a way to solve that exact maze. Lol.

  88. December 18, 2009

    Do you entered the studio to work ...? I do not understand

  89. December 18, 2009

    everything very open and transparent

  90. September 18, 2010

    you still had to know something about mazes (i.e. that they only have two walls) before you could jump to the solution. I didn't know that, so I thank you for teaching me something new

  91. September 23, 2010

    Thank you for good sharing.

  92. October 7, 2010

    Thank you for good sharing

  93. October 8, 2010

    nice collection

  94. fond42518@email.com
    November 27, 2010

    It doesn't work...

  95. December 15, 2010

    seems delicious like my cupcakes

  96. January 9, 2011

    good admin thanks.

  97. June 9, 2011

    Great tut. thx :)

  98. August 6, 2011

    Thanks for this awesome writeup. I will use this for my research paper so I have bookmarked this page! Thanks so much again!!!

  99. November 7, 2011

    hmmm searched for it a day long! ty

  100. November 7, 2011

    thx for sharing dude.

  101. Max
    January 29, 2012

    This is amazing stuff!

  102. April 20, 2012

    Heh, but that is not the point of the maze :) The point is to solve it yourself. However, still an effective technique for solving it fast! ;) You might try one of my mazes too!