 Hey, what's up everybody John Hammond here coming back at you with level 22 of the Python challenge So we just finished up level 21 here We are at the new link up based off of copper that was kind of a passphrase to get to this level So now we're greeted with this image of what looks like a joystick kind of like a game controller Nothing else on the web page. So let's view the source. I control you here Regular HTML nothing interesting There's just the image here level 2.jpg and an HTML comment that says or maybe white dot gif would be more bright Huh, okay. I don't know what that means, but I'm assuming that it's referring to this image here Maybe we can just you view a different image view that white dot gif So I paste that into my browser here and it just looks like a black box Not a white gif. I'm gonna control us to save this I'm pulling pie challenge folder created a new folder called level 22 and then Let's get a terminal open so we can Actually start to work with this stuff. So moving to level 22. Do we have the white gif? Okay, we actually have a white gif image cool. This definitely is a gif image. Let's do some regular reconnaissance on it I'm gonna run exit tool Stay white dot gif. It's a gif image got the right version image width and height is 200 200 Animation iterations is infinite. Okay, so if I try to view this thing I Guess this is an animation, but I don't see anything. It just looks like a black box to me. Okay Maybe we can use Python to Find something valuable out of that Let's try that right now. I guess let's see if we can get all the frames out of this thing So let's open up a new script I'll close everything that I've got right now And we can get started again user bin environment Python And now I have to actually like save this thing so I thought 22 8.py And let's do this in sublime text 3 because I don't want sublime text 2 to freak out about the The bill view stuff. Okay. Good sweet now. I have regular build. Okay. I I'm gonna be working with the pill or the Python image library. I'm gonna do that From pill important image so I can go ahead and image dot open White dot gif So I'll store it in an object. I'll just call IMG so if you wanted to we can do IMG dot show and Displays it for us 200 200, but I want to see how many frames are in this thing so IMG and frames and And a hundred thirty three. Okay, so for frame in Image dot and frames we want to range here because we're gonna loop through them so We can do image dot seek frame number and now the current image is going to be I'm pretty sure a Stale a stale image. Let's find out And then we can do image dot save to Let's call this like frame D dollar sign or percent D so we can just format specifier And format specify it with the frame number and let's convert this to a string So actually I don't need to do that because I can just use the format string without having to have to create anything crazy so Now if I run this do we have a bunch of more images in here we do okay? So we can look at all of these frames dot PNG like frame zero dot PNG Oh had a little too many extensions there and we can keep iterating through them in doesn't look like there's anything different but let's take a look at how this looks in It's Like it's color values right now. So let's just make this as a function Extract frames cool, so let's put that away for now, but let's create a Image kind of let's see this for frame zero Frame zero dot PNG. Okay. Let's create a pixel access object or like access the data of every image in just a data variable right here and let's call the Let's actually get the contents out of it So we're going to need the size and like the width and height like of the dimensions of the image to begin with So that should be image dot size So now we can do a for x in range width and Do the same thing for y in range of height So we can print out the data that we're working with at that position x and y and Let's just do this in the console. I haven't marked a executable yet. So let's do that and We get a lot of zeros as expected a lot of black So let's sort these and put them to unique taxi So we have a ton of zeros, but only one eight So that must be hiding because it looks very very much like a black pixel But we know that it's different from all the others So let's just set that equal to like two five five and then if Okay, we get a test for one thing if that Pixel that we're currently looking at is the eight if it is the outlier then we'll set that But if not, we'll just ignore it. So then we can do image dot show and Now it displays it You may not be able to see it that clearly But there is just a pixel a small small pixel in the very very center of the screen So a white pixel just right there now if we do that with all of the other frames. I wonder what will happen Let's make this go away. I'm just kind of going to do this With all the files since we have them right now Kind of be a little bit of a gross hack But I guess let's just see what they're all saved as and we can create another J 53 really wanted to see what happens with this Because it's just like one pixel right now So I want to see if it tries to spill anything out or tries to write anything or do anything crazy So let's keep track of the frames that we have here the frame number was like one three three right so Frames so for I in range Total frames now we can go ahead and use per dollar sign D or oh my goodness I keep doing that percent sign D and then I will be our iterator So we get the image original and then let's create a new image Let's call that image dot new New frame dollar sign D dot PNG with our percent specifier and Actually, that's not the file name that goes in there. It's the type So RGB and then it needs the size so in which case we'll use the size from the old image So we can say new data equals image Our new dot load. Okay, so we're getting in copied image here So if the current pixel in the original image is the odd lot like Outlier we can say our new data will set it to a 255 and we don't need to show this anymore but at the very end of the iteration we can say New dot save With the frame the new frame file in that we've got here. So if I run this hopefully it won't break Cool now if we a less we should have a ton of new frames. So I can EOG new frame and Hold that down and you can see I've got a red pixel there, but because it's RGB I want it to be 255 255 For white and then let's run that Just to clean those up a little bit now. I have a white pixel in the very very center of the screen So it looks like it's moving here and there I'm just gonna convert all these new frames into a final dot gif kind of a hack with image magic I know I'm not using real Python there. So I'm sorry But hopefully we can now just look at final dot gif and get an idea as to what this is doing because it looks like It's just a white pixel moving up and down And that's really it. It's not really spelling anything for us or doing anything crazy. So okay, um, I Suppose we can Go back to the drawing board We've got all these frame files, but I don't know if they'll actually help us because they're all just Just boxes with a white pixel in the center, but oh Hey, it looks like they're just kind of moving. I guess like a joystick would right? Yeah So maybe the joystick is trying to spell something out To begin with That must be it. How do we keep track of? I guess we got to try and figure out how we can keep track of the coordinates that it's doing here for us let's um Let's kind of remove these for now And let's just start over here. Let's say the original image equals image dot open White dot jiff. So we'll work with the jiff here and we'll handle all of the frames as we come across them so image dot n frames so for frame number in image dot n frames we want to arrange that here and Let's seek to it image dot seek frame number Sorry, that's a function. Okay, so now We are out the current frame and we can find out the coordinates of that Outlier pixel the one that has eight as its color value not zero So not real black, but just hiding kind of gray black. We can do that by What is the function? What is that function called pill image? bounding box Pill box get B box get B box calculates the bounding box of the non-zero regions in the image. Okay, so it'll return a for tuple defining the left upper right and Lower pixel coordinates so we could get x and y and I guess x2 y2 if we use Image dot get B box cool so for each of those frames What is x? and why Let's see how that looks Okay, so they're moving Right 100 100 is a center. So that's the start and It moves to it moves down down down on down I think and then to the right a little bit because the x and y is moving Okay, so just like a joystick. So we just have to follow We just have to draw the same thing it's drawing So hmm Let's create a cursor for us like cursor x cursor y. It'll start at the center so 100 100 yeah and We actually need a new image for one thing so new equals image dot new RGB and What's the size do you want it's gonna try it I? Feel like if it's trying to like spell something out like if it's trying to write something We're gonna do a little bit more space. So I feel like we're gonna have to Go like 500 or something Like a little more 400 is probably fine But just bigger than the original image like 200 by 200. I think I don't know Okay, so cursor x cursor y is gonna take the 100 100 and then we want to know the difference, right? So x difference equals Cursor x minus x Right. Yeah, and cursor y y difference. So as it moves we can make these changes to the cursor I think that should be right because As it's moving x difference is gonna be like Zero zero it's got to be the center. Oh We probably need a center there you though. Yeah, we need okay We just need center cursor cursor. We want to keep moving but center x and center y Will be I mean it'll be the same as cursor to the start with so I guess we can and I'm doing that so We'll be able to tell the difference in How a joystick is moving according to the image and then once we know the Difference we will add that to our cursor and cursor will change by difference so Now I guess we can just set a new So instead of pixel at our new image and display it out We so we got to get a pixel access object for our new so data equals new load and New Or I guess data cursor x cursor y Can equal a tuple of white 255 255 So at the very end of this we can go new show and Let's see if we moved along with it We drew a line Okay I don't know if that did exactly what I wanted it to do I Think think I did have this back. Oh for one thing. I had y here. So that's pretty bad and it should be The with the way we're gonna tell the difference is whether or not That white pixel they're offset from the center So we have to do the other way around it should be x minus center x and y minus center y I think Okay, cool. Now it's drawn stuff. I think but It's not really all that visible so It like would stop drawing something when it got back to the center, right? So we can just text we can just test like if If the cursor is Or like if the difference we want to know if the difference is zero So if it haven't if it hasn't moved from the center, so if x difference is equal to zero and y difference is equal to zero Then we will update the cursor position to like shift to the right So cursor x like plus equal 50 or something So that way looks like we're moving Oh, hey, okay, it spells out bonus Uh, okay, what's is that it? Is that just all we need? back to copper.html we can try bonus.html. Yes, all right Got to level 23 just like that Okay, a little tinkering a little a little playing around with pipe on little exploration. Hope you guys had fun with that one I thought that was pretty awesome I don't have a lot of code little little experimental code that we were working with kind of at the end Or at least through the beginning and then what we tinkered with at the end to Kind of write out what it was trying to draw with that joystick. So hey, that was cool I thought that was really neat. I Am glad I thought back to that get b-box function because that honestly saved a lot I don't know how we would have really been able to have detected Well, I mean, I know we could have detected you like okay While we do our loop if we're determined like okay It's this if this is the outlier pixel X you want to know what coordinates out You can see the X and Y values in your loop, but then you'd have to handle them and break out and that's nonsense So this image dot get b-box is pretty handy and anything really within the Python image library is super handy I'm it's that's an awesome weapon and arsenal to have in your bag. So Hey, man, thank you guys for watching. Hope you enjoyed this if you did like the video Hey, please do like click the like button physically do it that would be awesome I'm super grateful and if you're willing to subscribe if you haven't already if you really thanks for watching guys I'll see you in the next video when we jump into Python challenge level 23 We're just cruising through this stuff. So thanks again guys. See you later