Python Tutorial: Image Manipulation with Pillow
Skills:
CV Basics85%
Key Takeaways
The video demonstrates how to use the Pillow library in Python for image manipulation, including resizing, cropping, changing colors, and blurring. It covers various tools and techniques such as installing Pillow using pip, creating image objects, and modifying images using Pillow's API.
Full Transcript
hey how's it going everybody in this video we'll be taking a look at the pillow library for python uh so pillow allows us to work with and manipulate images using python uh so we'll learn how we can do several things with the images like uh 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 um so for example I have a a website that contains images and I usually need multiple sizes of these images uh one fullsize 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 uh you can do all of this automatically all at once uh 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 uh so I'm on a Mac so first you want to uh Google uh 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 OSX 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 uh so you also need a few external libraries as well so uh I use this Brew install command here to install these external libraries now if you don't have home brew installed on your machine then you will need to do that and there's a link here to do that as well um but I'm not going to go into too much detail about that right now um 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 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 uh but there's also no errors so that's a good thing now this may be a little different than packages that you're used to working with uh it's a little strange that we did a pip install pillow and whenever we use the uh package we say from pil import the modules that you want but I think this is a 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 uh 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 uh so I'll be able to reference them simply with their file names but 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 um 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. openen and then the file name that I'm going to grab over here is pup one. jpeg so I'm going to say pup one. jpeg 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. 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 uh save instead of a JPEG file What if I wanted to save that as a PNG instead uh well 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 pup one. jpeg I'm just going to call this pup one.png and if I run this code you can see over here in my file system that now we have this pup one. JPEG and pup one.png and I can open that up just like I did the jpeg okay so now I'm going to delete this PNG that we just now created now uh 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 pngs 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 pngs and this will be the folder where I hold the PNG versions of all of these jpegs okay so first we're going to need a way to Loop over all of the images and 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 fn. list 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 do ends with and then I'm going to say if it ends with a JPEG then at this point I'm just going to print out F so if we run this then we can see down here in 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 do 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 uh from the file that I have so I'm going to say os. path. split text and pass in the file name so now this is going to break everything up into a file name and a 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 pngs inside the PNG folder I'm just going to say I dove and then within here I can name the file so I'm going to say pngs to pass it into the pngs directory and then I will do the file name there do PNG then I'll do a 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 go 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 pgs directory so I just ran that now if I open up this pngs directory you can see that we have all four of these files that have a 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 I did on my website where instead of uh 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 resize version of all your files uh for thumbnails or image galleries so not only are we going to want to resize our file but we also want to keep the same aspect ratio too so that our photos don't get squished or distorted uh 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 uncore 300 and your image sizes need to be a tupal 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 DEC ided on a file size here in my for Loop right above where I'm doing the save I'm just going to say i. 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 an 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 say uh fext there to pass in 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 uncore 300 uh jpeg 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 uh pixel images as well uh 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 leaps through we're going to make the image object we're going to resize this to a 700 pixel thumbnail 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 are 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 in half a second if I go into the 300 pixel folder you can see that we have our uh 300 pixel images there and if I go into the 700 pixel folder then we have our 700 pixel images there so you can see how a library like this will be ex would be extremely useful and it keeps you from doing a lot of this work that some people do manually every time they upload images to their website and it's how I used to do uh images for my website too I would go in and resize these full-size 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 we' 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 uh 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 uh you can blur images and you can do all kinds of things uh 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 in that for loop as well and it would uh 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. jpeg so first let me rotate an image so to rotate an image I'm going to do image one dot rotate and let's say that we just want to rotate at 90° so now after we rotate at 90 ° 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 oneor mod. jpeg so if I run this code here then you can see we have this pup one mod and if I open this up you can see that that image has been rotated 90° okay now let's say that we wanted to make this image black and white now to do this instead of rotating 90° I'm going to do a convert and in inside what I'm going to pass into the convert I'm going to say mode equals L and save that and if I run that then you can see over here now our pup one mod if we open that up is a black and white image now just so you know I didn't memorize uh that convert and mode and L for the black and white 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 uh it's really not feasible to uh 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 uh so whatever you're trying to do just go to the pillow documentation and find the values that you need and um 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 uh let's go ahead and do one that's a little bit more complicated let's blur our image um so if we 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 that I can say image one. filter and within filter I'm going to pass in an image filter Dot and I'm going to do a gajian blur so if I go ahead and save this and run it if I open up this pup one mod here you can see that this doesn't seem very blurred and that's because we Ed 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 of this text and I'll make this bigger again uh so you can see that we just did a gajian blur with uh the default values and the default values and I found this from the documentation it's a radius set to two which it does blur the image but just not very much so if if I replace that default value of two 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 but if we close down that image and then open it back up you can see that that blur is uh 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 uh image library in Python I do recommend going and checking out the documentation because there's so much more that you can do with images uh than just what was in this video but if you do have any questions about this stuff just feel free to ask in the comment section below uh be sure to subscribe for future python videos and thank you all for watching
Original Description
In this video we will learn how to modify and manipulate images using the Python Pillow Library. Pillow is a fork of the Python Imaging Library (PIL). It will allow us to do many different things to our images such as: changing their file extension, resizing, cropping, changing colors, blurring, and much more.
Pillow is extremely useful when you have multiple images you wish to process at once. For example, you can use Pillow to automatically create different sized thumbnails of images you upload to your web server. Let's get started.
✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms
✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join
✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY
✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1
✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer
▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter - https://twitter.com/CoreyMSchafer
Instagram - https://www.instagram.com/coreymschafer/
#Python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Corey Schafer · Corey Schafer · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Web fonts using CSS Font Face
Corey Schafer
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
Paver Patio Time Lapse
Corey Schafer
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
WordPress Plugins: Imsanity
Corey Schafer
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
Python Tutorial: if __name__ == '__main__'
Corey Schafer
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
Easily Resize Multiple Images Using Picasa
Corey Schafer
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
Git Tutorial: Using the Stash Command
Corey Schafer
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
Git Tutorial: Diff and Merge Tools
Corey Schafer
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
Python Tutorial: str() vs repr()
Corey Schafer
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
Programming Terms: String Interpolation
Corey Schafer
Programming Terms: Idempotence
Corey Schafer
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
Programming Terms: Mutable vs Immutable
Corey Schafer
Python Tutorial: Else Clauses on Loops
Corey Schafer
Overview of Online Learning Resources
Corey Schafer
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
Programming Terms: Combinations and Permutations
Corey Schafer
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
Setting up a Python Development Environment in Eclipse
Corey Schafer
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer
More on: CV Basics
View skill →
🎓
Tutor Explanation
DeepCamp AI