 Right so hey guys and welcome back to another Python tutorial. So in this video I will actually be showing you guys how to create an image downloader which will help you download images from the internet. So what you'll be doing is passing the URL of the image to the program and the program will be downloading that image and saving it to the directory that you specified to do so. So without further ado, let's begin. So as you may see already I've got Visual Studio Code opened up. What you need to do now is go ahead and create a new file and then save that file as whatever file name you want. I have called it image downloader.py already so I'm not going to do that again. Tap in that .py and save. Cool. So let's actually begin. So in this tutorial we're going to be needing to make requests to different websites since we need to grab the URL, make a request to the specific website from where the URL is and then get the information about that image. So we're going to do import requests because we need to make a load of requests for this and we're going to create a function called download files. And then in this function we're going to pass in a URL. So this is our custom functions that we're creating for ourselves just to make our code a lot more neater. So in this function we're pretty much going to be needing the URL to the image and the function is going to save the downloaded file in the end as an outcome. So what we need to do first off is actually find out a local file name that is going to be used to save the image once the data for it is downloaded. So local file name is going to be a variable where it's assigned to URL. So whatever URL is passed as a parameter to this function .split and then fold slash and then access the array and use minus one. So what does this do you may ask? I'm going to quickly print local file name and now I'm going to go down here, use my download files function and type in fake URL. So let's say .com and fold slash is going to be something like an image .png. Cool. So let's assume this is the fake reference to an actual image online but it's obviously not. So what we are going to be returned is going to be the file name including the extension of the image. So in this case it's going to return to us image .png which is what we are going to be using to actually save the file onto our computer. So let's run this. Seems like it's running something else. Let's delete that. Run this again. And as you see right here we have an outcome that says image .png. Now if I were to close this and then change it to I don't know image one .png because obviously links will change all the time. Save that and run it up again. It says image one .png. So no matter what the URL is it's going to capture the file name and the extension type so that it can actually save successfully onto your computer. So that's one step done. We are going to get rid of this print local file name because we don't need to print it each time it finds it out. Now first of all what we are going to do is actually create a stream for our request. So with request.get as I said we need to get a lot of information for my URL which is going to be getting the actual data to save it later on. So we need to use the get protocol which is a common HTTP protocol to get information. We are not going to get into the other ones for now. We are just going to use get because we want to get information. Now we need to pass in the URL from which it's going to be grabbing the information and then we need to assign stream equals truth. So that will allow it to stream information. Now we need to assign this object or the value that's returned to SR so that we can reference to it a lot easier. Now at this stage the program is downloading data from the internet. So we are going to prompt the user that the program is downloading information. Cool. Now what we need to do next is actually prepare our file and create a new file with the local file name that we saved a moment ago. So this file is going to store all the information that's downloaded and then be converted into an image file to pretty much replicate the online image. So another file stream with open C. I'm going to give it a directory where I want it to save my image. You guys can go ahead and give it the directory where you want it to save your image. Don't copy this because that will not work for you. And then at the end of it I'm going to do a plus local file name so that it's concatenated the file name and its extension type and actually is able to write to the image file. We need to open it using write bytes mode because the data we're going to receive from the request object is going to be in bytes so we need to be able to write bytes to this file stream that we just opened. And then we're going to assign it to as F so that we can easily reference it. So we're going to print at this stage we're writing data to file. So we're going to prompt that to the user so that the user knows what's going on. At this stage what we could easily do is write f.write r.content. So we could just do that and then we could do f.close and the program would work just fine. But the issue with doing that is that it's bad practice. So what we're doing right here if we had to do it like this would be that all the data that's just gathered and downloaded into this request object we would be taking all of it shoving it into our RAM and then writing it all at once. So that could put a lot of pressure on the computer. We don't want to do that because it's bad practice as well. So what we can actually do instead is break the data that we've just downloaded from the internet into chunks and then one by one we take that chunk of information and keep writing it to our open file until the data has actually been completed for the file to be saved as an image. So we are going to use a for loop and then we're going to save for chunk which is just a variable to store each chunk of information in r.iterateContent. It's built right, not enter, it's a content. So what we're saying is for each chunk of information in the r object which is the request object which holds our data for each chunk of information in that object we want to iterate through the object and we want to pass in a parameter here which is going to include the chunk size. Now the chunk size is going to let us select pretty much how much information we want to write to the file at one time. You could go with 1024 bytes because it's a pretty good one to use but my system is pretty strong I'm going to go with 8192 bytes at a time. It's not a lot to write to an image but hey I'm going to go with that much for now. So the chunk size is pretty much how much data we're writing to our file in each iteration of this loop. So the higher the number the quicker the file would be saved, obviously it's going to be in milliseconds difference unless your image file is humongous which I doubt it will. Now next use a colon and then finally you're going to write the information to the file. Now while writing it you're not going to do r.content because that's the entire chunk of information. Not the split one we're going to actually use the variable chunk which is holding each chunk of information that's gathered after each iteration of the loop until the loop is completed and the file data has been saved. Cool now once this is written we need to make sure that we're actually closing the file as well so we go three iteration I mean three indents back and then type in f.close so that the file is closed and all the data is saved. Now we print download complete as obviously the download is complete you can also add on to that by saying something like file saved as and then we type in the local file name variable that's a nice touch to it. So now without further ado let's actually go ahead and run this function but this time with an actual legit link to an image. So if I go to Chrome I've actually got an image selected right here so what I did was I just typed in images on the internet and then I copy link address right here make sure you're copying the right one so copy link address minimize the window I'm gonna go in here and paste the link cool now once of actually I'm going to go back because that doesn't seem like the direct link to the image so I'm going to go ahead and open image in new tab and then when you open it if it's the right link you would see a .jpeg at the end right so you need to make sure that you have an actual extension in the link otherwise this will not work so make sure that the end of the URL actually has an extension for the image control a control C to copy the link I'm gonna get rid of this stuff because that's not the one we're looking for and we're gonna paste the actual one so make sure it has an extension at the end save it and now I'm going to quickly go ahead and run this now let's maximize this to see what just happened as we programmed it to do it said downloading first now this is all happened so quickly because my internet's decent speed as well so it downloaded it in seconds then it says write it in file to data which is when the chunks of data are being written to the file one at a time and then lastly when the file has been saved and closed it just says download complete which is telling us that it's finished the download and lastly it says file saved as whatever the file name was .jpeg now moment of truth did this actually work let's find out I'm gonna minimize this and where is the image okay it doesn't seem like it worked so I'm gonna refresh the window just to make sure that it's actually working let's run this again okay that's weird let's try that again let's just try again I guess I'm not sure why that didn't work okay there's no image let's actually try debugging it by changing this plus local file name to just a static file name so since I already know that this is gonna be a what's it PNG or JPEG JPEG file I'm gonna go ahead and just do something like your hand jpg let's run it now let's see and here we are so apparently it was an issue with actually grabbing the file name so as we see right here I have the image downloaded to my desktop saved as your hand jpg and we have the exact image from the internet now we're gonna actually try and figure out why it didn't work with the local file name weird so I'm going to get rid of this and do my plus local file name again run it up okay this time it seemed to work maybe the last time I forgot to put the forward slash so this time it seemed to work let's test it with another image URL cuz here we are with the actual URL it's meant to save it with let's test it with another URL let's go ahead and find another image let's go with this one open image in new tab does it actually have an extension this one doesn't so we're gonna go for another one let's try this one hopefully this one does okay this one has an extension so I'm gonna copy this link right here so direct link and made sure that actually actually has an extension to and I'm going back to my code I'm gonna replace this link now with my new link save and run it says file saved let's close this off and as you see right here guys we have actually managed to pull this off so our images are being downloaded and saved to the location I've asked it to that's a bit annoying to keep having to go back in the function each time and doing this individually so we're gonna put this whole thing in a loop so I'm going to get rid of this and I'm gonna type in a short loop so while one which is an unstoppable loop until these actually quits the program welcome to the image downloader and then we're gonna grab the image URL from the user by capturing as an input so input string let's say image URL and the user is gonna enter the URL there once the URL has been captured we're just gonna type in our download files function and pretty neat we're gonna type in image URL through there and in the download files function to do the rest lastly we're just gonna print a blank line let's run this up and before doing so I'm just gonna kill this terminal because it's messy let's try it now now it's asking it's saying welcome to the image downloader which is pretty nice image URL the question is do I still have it okay I do so that's my image URL I'll press enter it says downloading writing data to file download complete file saved as tree whatever dot jpeg did it actually work yes it did now let's tell you what on a run and you wanted another image just back to back let's open up Chrome again images let's go back to finding another image let's go for this one open image a new tab this one doesn't actually have a file extension so that's not gonna work out open image a new tab this one does so I'm gonna copy the link to this and then I'm going to paste it in my image URL just to check that it's working twice in a loop press enter downloading writing data and saved as dog whatever it is now let's go back to my desktop and see if that actually worked and as you guys see my two images are actually saved so that was it for today's tutorial guys hope you've enjoyed this different experience of working with web files and requests on the web watch out for future tutorials which I'm gonna be explaining how to use flask and Django as well so we're kind of going to jump into the web and start doing a bit more complex stuff in the future I'm also going to be releasing a tutorial on how to download youtube videos using a direct link in the future because that's a lot more useful than just downloading images because with images you could just right click and click on save as and it's done but with youtube images it'll be pretty cool if you could have your own app that does it for you either way guys if you have any suggestions for new videos please drop them in the comments thank you for all the support that you have been showing me in the recent tutorials I really do appreciate it if you'd like to directly support the channel you can do so by either joining as a patreon on my patreon page so you can pay a certain amount to be a member but i'm not forcing anyone if you feel like doing it you can do you can also purchase a super chat emoji or a highlighted message do consider also joining the discord channel because it's a lot of fun on there it's just a lot of chill people that you can speak to and troubleshoot errors or provide ideas for new videos and guys i will see your beautiful faces in the next tutorial peace