Hey, how’s it going? Everybody in this video, we’ll be taking a look at the pillow library for Python, so pillow allows us to work with and manipulate images using Python, so we’ll learn how we can do several things with the images like displaying them to the screen, resizing them, modifying their colors and also saving them back to your machine. So this is extremely useful. When you want to modify a lot of images at once or if you want to automatically run a script on images that you upload to a certain directory, so for example, I have a website that contains images and I usually need multiple sizes of these images, one full-size image and then several others for thumbnails and image galleries and things like that. Now I used to manually resize all of the images, and it took a long time, but with a library like pillow. You can do all of this automatically all at once, so let’s go ahead and get started and see how this works so first off. We need to install pillow now. I already have this installed on my machine, but I’ll show you the commands that I used in order to get it running, so I’m on a Mac so first you want to Google pillow for Python and it will take you to their documentation and since I’m on a Mac, you can install it for whatever operating system you’re on, but I went down here to OS X installation. Now it’s pretty easy to get this working on your machine, but it’s not quite as easy as only running the Pip install, so you also need a few external libraries as well, so I use this brew Install command here to install these external libraries now. If you don’t have homebrew installed on your machine, then you will need to do that, and there’s a link here to do that as well, but I’m not going to go into too much detail about that right now. So once you get these external libraries installed, then you can just run this Pip install pillow command and that should take care of everything else now. Once you think that you have that installed correctly, then you should be able to open up the editor of your choice here and in my case. I have sublime text open up here and you should be able to type in from Pil import image. So if I run that you can see that my script ran with no output, but there’s also no error, So that’s a good thing. Now this may be a little different than packages that you’re used to working with. It’s a little strange that we did a Pip install pillow. And whenever we use the package we say from. Pil, import the modules that you want, but I think this is it from a naming convention. That’s just kind of been around for a while, so it’s how they still do it so now that we have pill installed, Let’s start playing with some images. So I have a couple of images here of my dog. Whenever he was a puppy that we can work with now. These images are in the same directory as my Python file here, so I’ll be able to reference them simply with their file names. If you’re working with images that are in a different location, then you can just use their full path to the image so first things first, let’s start off really simple and see if we can just display one of these images to the screen, so we’re going to need to create an image object and I already have image imported here so now to create this image object. Let’s just call this image one, and I’m going to set it equal to image Dot open, and then the file name that I’m going to grab over here is pup one but JPG, so I’m going to say pup one dot JPG. So now this gives me an image object that I can work with, and you can do just about anything with this image object. So what we’re going to want to do is we’re just going to do image One dot show. Now if I run this command. You can see here that my Python script here did pull up this image and open it up in preview, so let’s go ahead and close that down. So the fact that we were able to display that image to the screen. Now we know that we’re working with the correct image, so let’s go ahead and do a simple modification to it. So what if I wanted to save instead of a JPG file? What if I wanted to save that as a PNG? Instead, I want to do that. We can use this. Save method now within the save method. Here I am just going to grab this file name here, and instead of up one, not JPG. I’m just going to call this pup. One dot PNG. And if I run this code, you can see over here in my file system that now we have this pup 1 JPG and pump 1 Dot. PNG and I can open that up. Just like I did the jpg okay, so now. I’m going to delete this PNG that we just now created now now. This is where the pillow library really comes in handy is whenever we want to work with multiple images at a time. So what if we wanted to convert all of these images to PNG s? So let’s go ahead and do that? I’m going to create a new folder here in my file system, and I’m going to call this P&G s and this will be the folder where I hold the P&G versions of all of these JPEGs. Okay, so first we’re going to need a way to loop over all of the images in our current directory now to do this. I’m going to import the OS module now. Since this is a tutorial on the pillow library. I’m not going to go too in depth as to what the OS module is doing here but to loop over the files in the current directory. I’m just going to say for F in OS dot list, dur. And then I’m just going to put a dot here for the current directory, so that for loop will loop through everything in the current directory. So now I’m going to say if F dot ends with, and then I’m going to say if it ends with a dot JPG, then at this point, I’m just going to print out F. So if we run this, then we can see down here and our output that it printed out all of our JPEGs that we have in the current directory. So now that I know that that’s working correctly, I’m going to go ahead and make an image object out of each one of these, so I’m just going to say I equals image Dot open F and save that. So now I’m going to be saving all of these as a PNG file, but I want to keep the same file name, so I’m going to go ahead and split out the file name and the file extension from the file that I have, so I’m going to say Oh, s dot path dot split, text and pass in the file name. So now this is going to break everything up into a file name and file extension. So if I print the file name and run this, then you can see that we just got the file name without the extension, and if I print the file extension and run that then you can see that we just get four JPEGs so now to save all of those as Ang’s inside the PNG folder. I’m just going to say I dot save and then within here, I can name the file, so I’m going to say P and Gs to pass it into the PNG sector II, and then I will do the file name there, dot PNG and then I’ll do a dot format to pass in that file name and Ill. Save that and this should be a dot right here. That’s typo so now if I run this code, what it’s going to do is it’s going to go through and make an image object out of each of these files and then save it into this. P and Gs directory. So I just ran that now. If I open up this PNG directory, you can see that we have all four of these files that have a dot. PNG extension on them. Okay, so that’s one thing that you can do with a group of images, but let’s try to do that example. I was talking about before that. I did on my website where instead of giving all the files a different extension. Let’s see if we can resize each file So like I said, this would be extremely useful on a Web server or something similar. If you want a resized version of all your files for thumbnails or image galleries, so not only are we going to want to resize a file? But we also want to keep the same aspect ratio, too, so that our photos don’t get squished or distorted so first let’s decide what maximum size we want our files to be so for this example. I’m going to say that I want the size of our files to be. Let’s say 300 so I’m going to make a variable here. That’s size underscore 300 and your image sizes need to be a tuple of the sizes, So I’m just going to say 300 comma 300 so now in my file system over here, I’m going to go ahead and create a new folder and I’m going to call this folder 300 and this will hold all of our 300 pixel file sizes. So now that we’ve decided on a file size here in my for loop right above where I’m doing the save, I’m just going to say I dot thumbnail and I’m going to pass in this size of 300 here now. I’m also going to modify my save down here to save into the 300 folder. Then I’m going to go ahead and do the original file name with underscore 300 there, and instead of this PNG extension, I’m just going to go ahead and keep the regular extension and pass that in using the format, so I’ll save FEX T there to send that file extension. So now if I run this, what this does is, it goes through, and it makes an image object out of all these files, and then it does a thumbnail to the size of 300 300 that we made up here and then it saves this into the 300 directory. So if I go over here and open up our 300 directory, you can see that we have our old file. Name underscore 300 G Peg. So if I open up all these, you can see that now. These are a small 300 pixel version of the images of our original images. Now the great thing about being able to do this in Python is that we can just quickly and easily modify our script to accept any new sizes that we want so say, for example, that we suddenly had a requirement to do 700 pixel images as well so we can go through and just as easily add in another step to do 300 and 700 pixel images. So if I change this to 700 here, then what I’m going to do is I’m just going to copy all of this and then go down here and I’ll do a size of 700 up here at top, and then after it makes the 700 image, then it’ll go through and make the 300 size image, so you can see that we have two different steps here, so when it leaks through, we’re going to make the image object, we’re going to resize this to a 700 pixels, Um, nail, save it into a 700 directory here, which I’m going to go ahead and create, so I’ll create that there and then we’re going to resize it to a 300 pixel image and then save it into the 300 pixel folder. So if I run this, you can see that finished and half a second. If I go into the 300 pixel folder, you can see that we have our 300 pixel images there, and if I go into the 700 pixel holder, then we have our 700 pixel images there, so you can see how library like this will be would be extremely useful when it keeps you from doing a lot of this work that some people do manually every time they upload images to the website, and it’s how I used to do images from my website, too. I would go in and resize these full-sized images down one by one and then upload them to my Web server, but using a library like this makes all of that so much faster, but so far, we’ve only touched on a few things that you can do with this imaging library, so let me show you a few more things just to give you a few quick ideas before we end the video here, so for example, so far we’ve gone through, and we’ve changed file extensions and we’ve resized the images, but you can also rotate images you can make images, black and white. You can blur images and you can do all kinds of things, so let’s just walk through a couple of these examples, so I’m going to go ahead and remove this entire for loop here, and I’m just going to work with one image at a time, so it’s a little bit more obvious what’s going on, but you could just as easily do all of these and that for loop as well and it would do that to all of your images. Okay, so let me go ahead and uncomment out this code where I was working on this pup, One dot jpg so first, let me rotate an image so to rotate an image. I’m going to do image 1 dot rotate and let’s say that we just want to rotate it 90 degrees so now after we rotate it 90 degrees then. I’m going to go ahead and run the save method on it. And I will just call this. I’ll call it Pup 1 underscore Mod dot JPG. So if I run this code here, then you can see. We have this pup 1 mod. And if I open this up, you can see that at that image has been rotated 90 degrees. Okay, now let’s say that we wanted to make this image black-and-white now to do this instead of rotating 90 degrees. I’m going to do a convert and inside what I’m going to pass into the convert. I’m going to say mode equals Ill and save that. And if I run that, then you can see over here now. Our pup 1 mod. If we open that up is a black and white image. Now, just so, you know? I didn’t memorize that convert and Moe. They’ll for the black and wine. I had to go and look that up in the documentation to get those values. And when you first start working with libraries like this, it’s really not, it’s really not feasible to think that you’re going to memorize all these off the top of your head, so you really have to get comfortable with the documentation, so whatever you’re trying to do, just go to the pillow documentation and find the values that you need and and do it that way, it’s a great resource to learn what you can do with images, but just to give you a further idea of a couple more things that you can do with these images. Let’s go ahead and do one. That’s a little bit more complicated. Let’s blur our image now. So if you want to blur an image, then we have to import another module up here. So instead of just image, let’s go ahead and also import image filter. So now if I want to blur my image back and say image 1 dot filter and within filter, I’m going to pass in an image filter Dot, and I’m going to do a Gaussian blur, so if I go ahead and save this and run it if I open up this up one Mod here, you can see that this doesn’t seem very blurred, and that’s because we use the default values with this blur. Let me go ahead and make my text a actually. I’m just going to spread this over a little bit so that you can see all this text and I’ll make this bigger again. So you can see that we just did a. Gaussian blur with the default values and the default values and I found this from the documentation. It’s a radius set to 2 which it does blur the image, but just not very much, so if I replace that default value of 2 with say like a 15 and then I rerun this code now let’s close down this image, and we kind of already got a look at what it looks like if we close down that image and then open it back up, you can see that that blur is is much more blurred than it was before so. I think that about does it for this video. I hope that gives you some good ideas for what you can do with this image, library and Python. I do recommend going and checking out the documentation because there’s so much more that you can do with images than just what was in the video. But if you do have any questions about this stuff, just feel free to ask in the comment section below, be sure to subscribe for future. Python videos and thank you all for watching.