Python Tutorial: Image Manipulation with Pillow

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 G's 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 I'll 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 G's 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
Info
Channel: Corey Schafer
Views: 305,858
Rating: undefined out of 5
Keywords: Python (Programming Language), Tutorial, Python Pillow, Python PIL, Python Imaging Library, Python Tutorial, Python Pillow Tutorial, Python PIL Tutorial, Python for Beginners, Python Images, Image Manipulation, Image Modification, Python Processing, Python Image Processing, Image Processing (Field Of Study), Photo Manipulation, Learn Python, Programming, Programming Tutorials, Web Design, Web Development (Interest), Programming Language (Software Genre)
Id: 6Qs3wObeWwc
Channel Id: undefined
Length: 15min 48sec (948 seconds)
Published: Tue Nov 17 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.