 Hey everyone and welcome back to another Python challenge video. We're on the 14th challenge and staring us in the face is this picture of a Swiss roll, I think I Don't entirely know so the URL is Italy I guess like that's just a location of where we are now on the website And I don't think that'll really be able to give us any hints or any clues But I mean that's what it is Below the image is this another square image. I don't know what it particularly is either It's just like a bunch of vertical lines. So as always, let's just do some investigation. If we right-click view the page source Shrink this down as you guys can see it the title of the page You guys can't see it in the actual page itself is is walk around So walk around Italy, I guess I would think that that I Mean whatever that means To walk around I guess going in a circle or go through kind of loop maybe loop through and Down at the bottom other than the Italy image is the other image the wire I guess is called wire dot PNG set a width of 100 and a height of 100 and This hint comment here says remember 100 times 100 equals I don't entirely know what that's supposed to refer to If we check out wire dot PNG. Oh Okay, I'm gonna try and zoom in here. I Guess I can't But you can see it's oh, it's not a hundred by a hundred like it says in the source That's just HTML formatting to make it that way looks like it's 10,000 Pixels in the X direction 10,000 pixels horizontally only one pixel tall So it's it's a wire. It's like a straight wire like that the source was saying Does it say the same thing here wire dot? PNG yeah Now if I try and zoom in It's just a bunch of weird things Okay, I Only think you know This is just a hunch and a guess that walk around we've got this wire and The picture is of like this spiral kind of Swiss roll thing like a spiral. Maybe it wants us to walk around this wire in a spiral Kind of like that sort of thing or like because I mean we can't do it With the wire at this 100 by 100 image Because it's not really it. It's it's like this wire that we have to wrap around and make a spiral I mean, I guess we can do it. I guess we can try it. I'll save this image Put on my desktop wire dot PNG I'll hop on over to my sublime text editor, and I'll save a new file as 14 oh wow 14 not not that PNG. I'm stupid 14 dot pi Okay, so We've got these images and that's what we're going to be manipulating. So of course we're going to need the image module import image And let's open up our wire a wire equals image dot open PNG and with the read formatting now If I go to my desktop here, there's our wire And it's this big 1000 or 10,000 by 100 size picture So if we try and wrap this into a We try and wrap it into a 100 by 100 image We're gonna need a separate 100 by 100 image, right? So let's build that. Let's say image quick stuff image dot new And of course you need the format of the image RGB Doesn't need to have any alpha channels and 100 by 100 is gonna be our size Okay So let's try and think about this if we want to wrap around this spiral sort of thing it's got to be What way is the spiral going it can be there going counterclockwise or clockwise? this one from the very back and going in it looks like clockwise So if we did this with the square with the try to wrap around a wire we'd be going like left to right and then Down and then we'd go back with right to left and then back up, right? Yeah, and Then we do the same thing Over and over again, but we would not cross over what we've already reached So the the minimum x and y values and the maximum x and y values will change as we traverse through our spiral I think So we do need minimum and maximum values is I think What I'm thinking let's let's set those up x-men and x-max. We'll set them equal to zero and I know we're thinking we'd use a hundred right But if we're gonna use this to actually index specific like portions of the wire image We have to set it to 99 Because if we try to do like wire get pixel or something If we were indexing at a specific place if we tried to use 100 It's not gonna know because it wants it to be at 99 because if it goes from zero to 99 that in itself is 100 places not Zero to 100 being would be make it 101 I think Yeah, that's that's at least the Rationale behind what I'm why I'm doing that But that's because we're indexing like that like we could use pixel access objects like you know when you like did image and Then out of specific coordinates, but I'm just gonna use to get pixel and put pixel because I think that's a little easier At least for now Pixel access objects you should use in general, but I don't want to set it up right now I'm just I'm just too lazy and a little rusty with the image library. So okay now we need our y-men and y-max Set them also equal to zero and 99 and Okay, if we're gonna be spiraling We're gonna need a Position or something that keeps track of where we are in both the x and the y-coordinate Like we could be Right here this one right here and it would need to know that it would be so far along in the x-direction And so far along in the y-direction and when we get to it farther and farther along like if we would be over here In the spiral that I'm not I'm not gonna finish drawing because I would just be a while it would also know that okay This is where we are so we can set that up. I think we're just like a coordinate thing Like x or y so I can't be a tuple or a tuple though because we have to be able to change it So I'm just gonna set a position variable to be a list or an array With what would be filled with x and y coordinates, but for now since we're initializing it Let's just set it equal to zero And on top of that we need the actual wires position, right? So that can just be an integer I'll call my place and Since it's only going horizontally like it's only 10,000 care 10,000 pixels in the x-direction That's all we need to keep track of just the x position So Okay, now when we're traversing across our spiral, let's actually get to the loop that traverses across our spiral for us X in x min and x max along with y min and y max should like Change as they go along and they'll end up meeting in the dead center of the spiral Like you'd go so far along this way and then you go so far along this way in this way in this way in this way Etc. Etc. Until you get to the very center of the of the spiral and in a square That's the very center of the square So they'll both be equal Right Yeah So let's set up the loop I'm gonna use a while loop rather than a for loop because that way I can just set up a condition and While not these things are equal to each other x max equals x min or Y max equals max is equal to y min, sorry But keep in mind we have our offset here from 99 and 99 So I'm actually gonna separate this because if they if they are equal that means we've gone too far that means we've actually crossed like They're at the exact same position and you obviously you can't get anything out of that so When we testing for the max value, we're gonna subtract one form it from both x and y so Okay Now that we've got our loop Now we can actually start moving in the x and y directions like traversing through our spiral and we'll do that with the for loop This while not is just gonna keep it going Over and over and over again, but our for loop is what's actually going to be stepping in one direction or another So we can set up let's let's go in the left to right direction first like the very top of the spiral We'll do for x in range X min because x-men is gonna have to change so we wouldn't set zero anything else here and then x max and Remember we want to go to the 100th place here So we are actually going to add one because of our offset The way that our computers count And that's all we need So what we would do now is we'd have x and y So we'd print the value for x Which we're gonna actually keep track of in our position array, right? so I'm gonna set it position zero and We'll do the same thing for y and that will be position ones. This is just displaying it out Obviously, we're not sending anything yet But that's so we can at least kind of figure it out. We can visualize and see what's going on your position zero Like we said before is going to be set to that x because we're moving across the top here place is gonna increment because we're moving along the wire and What we can do is we can grab the pixel in the wire Right here One of the very like far far left and it will keep going and going along as we traverse through this wire We'll actually get that and put it in our image The new one that we're building at the current position. We're at right So let's check that out. Let's do image put pixel now Normally, you should be using pixel access objects, but I'm just gonna do this quick and dirty So the position that we want and the actual image and we'll get it from our wire get pixel With place being the x position and then with zero because we don't need anything for the y and At that point Why man will be smaller because like if you think of it at the very top We're moving left to right at the very top of the spiral So when we get back to the top and we're moving to the left We can't start that far ahead in the y direction anymore. We have to stop We have to start at this second row not the first row Vertically in the y in the y direction the y aspect of that We have to increase the minimum value right because it's going it's counting from zero one two That sort of thing vertically down So hopefully you guys can visualize that and it's piecing together the same way in your head as it is in mine I know this one is kind of a little bit harder to visualize. I want to run this code Okay, cool. So it did what it did work for us Um, so yeah, why is going to stay the same as we go through all the stuff blah blah blah blah Um and acts is going to keep increasing. Okay, and it did this over and over and over again I think yeah, because uh why men is increasing We didn't actually cap our loop here, but that's okay I just wanted to show you guys how it was working. Why I was doing it that way So now we'll actually start working down since we worked We have worked left to right now We'll start working down in our spiral and we do this the same way except with why so for why in range Why men why max plus one and I'm gonna have dice and my same printout dialogue So I know what's going on here Um Set up a new variable like control this sort of thing output is going to equal true if output Okay, so now we can easily turn the output on and off Um, and now we'll continue But position one the second Uh index in our position array is going to equal why Place is going to increment And we'll do the same exact thing with our put pixel cool Now at the end of this loop x max is going to be subtracted one because if we're working from the Uh I want to try and visualize this for you, but I can't I hope you can visualize it with my cursor here I'll move it around a little bit if we walk across the spiral from left to right Until we get to the very end of it then we start moving down Now we can't move to this far right side of the image or of the spiral So x max is going to be minus one cool So now we get to work backwards now when we went from left to right and then uh down Now we're going to work right to left. We're going to work backwards in the horizontal direction So we can do this in a kind of a cool way for x in range x max we're going to start at the farthest that we can from the right corner that we've built for ourselves x min minus one Now this has the exact same offset for the same reason as the other ones And we're for our step value now now we can actually pass in a new argument to the range function as to how we're going to step through all this By default it's it's one and we're doing that as increasing in an ascending order Now we're going to go in a descending order with a negative one because we're going to work backwards horizontally So we're going we're increasing negatively. It's like for subtracting All right, um, and we'll do the same thing I think uh With the horizontal direction and that should print out perfectly fine for us Places increasing position is still the same at x So yeah, that works fine. And then now y max is going to go backwards, right Yeah, y max is going to decrease because we can't go that far at the bottom anymore Because when we're spiraling we're traversing right to left at the bottom of where our spiral is So a lot of this code is kind of duplicated and I'm sure you can figure that out, but I'm Well, I'm just kind of whipping this out It's for Readability or at least for being able to follow the flow of logic and see seeing how it's actually making this spiral Explicitly I guess so now we'll do the same thing with y working backwards um Y min minus one the same way we did x min minus one and of course we're decreasing so The same thing that we did with the y And now we should be able to do x min plus one because we can't go to that left side anymore. So What else is there? we've gone through the entire spiral from left to right and then Up and down From the top to the bottom and then right to left then back up And it does this over and over and over again because we set up this condition While these things are not equal to each other So let's try it. Um, we've got to save our new image image dot save Oh, okay, it's it's i am g not forward image And whatever file then we wanted to save as let's just call it spiral It's a png cool. So if I run this You're going to see all of our output just like go crazy on the screen right here And now if we kind of walk through it we can see That first it goes through x Like going left to right across it and then it keeps that position in x and goes down with y And now works backwards from right to left with x and then it goes down with y And then it does the same thing keeping the new Position because we set up x max and and x min and y max and y min They keep track of this stuff for us And we're just detracting from it every time or adding to it every time we need to as we go through another kind of layer or spiral in the overall Spiral if that makes any sense whatsoever So hopefully you can kind of understand what it's doing and seeing it output it with this x and y stuff It's kind of I think kind of cool So And if we just want to turn output off we can set output equal to false Now if I run this stuff, yeah, okay now, it's not completely gone So if I check it out on my desktop, we still got a little wire here the thing that was our source And thanks to our code. We've got this spiral dot png and it's thumbnail Looks like a little cat Okay, and I guess our code isn't perfect. I think we got a little blot Uh a black spot in the very center. That's that's okay. I mean we got pretty much the whole image that we need because hey, we got a cat um Then it's it's literally just a cat I guess Uh, so let's see what we can do with our Python challenge now um Let's just try and change the url to cat Is that it? Hey, ooh And its name is uzi you'll hear from him later Is that the same picture as the thumbnail? Pretty sure Yeah Yeah, that's the very same thing Its name is uzi you'll hear from him later. Uh, that's not a challenge What am I supposed to do here? um Okay, I'm just trying to change the url uzi.html how about that? Hey, yep, okay That one works now on challenge 15 sweet. We did it guys cool So, uh, that's all that I mean that's all that I'm gonna show you we got to we got through challenge 14 kind of a kind of a cool methodology or thing that we built here and working through this spiral and uh Hopefully it's readable. Hopefully you can understand the logic as we step through each one of these I know it's kind of duplicate code, but hey, that's okay. You can shrink it down if you want to but this is a quick and dirty method to Build our spiral and move on to the 15th challenge. So thank you guys for watching. Hope you enjoyed this Hope you're digging the python challenge series If you are maybe like the video maybe uh, maybe leave me a comment So i'm gonna instruct you criticism and if you're feeling willing subscribe. Thanks everybody. I'll see you in the next tutorial