Introduction to Pillow6:03 with Kenneth Love
Pillow is the successor to PIL, the Python Imaging Library. Let's go over some of the basics of the library and how it lets us work with images.
External libraries installation guide. These are libraries you may need to install before you can open up specific image types on your computer.
Common terms and methods
Image - Class for accessing images.
.open(path) - Open an image for manipulation
.rotate(angle, resample=0, expand=0) - Rotate an image by
angle degrees. If you set
True, the resulting image will be big enough to view the entire original image.
.resize(size, resample=0) - Resize an image to the width and height specified in a tuple like
.thumbnail(size, resample=1) - Resize an image so it fits within the width and height provided in the
thumbnail changes the
Image instance and doesn't return a new image.
.copy() - Copies the image to a new variable.
.crop(box=None) - Cuts out the specified box from the image. The box is a tuple of four numbers in the following format:
(left, top, right, bottom).
.save(file, format=None, **params) - Save the
Image to a new or existing file name or file object.
format lets you override the format of the image.
.paste(image, box=None, mode=None) - Pastes an
Image into another
Image. The pasted region must fit totally within the bounds of the pasted-into
Image. It'll be placed into the provided
.transpose(method) - Let's you apply some preset rotations and reflections to images.
[MUSIC] 0:00 Images are great until there's something wrong with them. 0:04 Maybe they need to be rotated or 0:07 cropped into a thumbnail or maybe you just want to desaturate them. 0:09 Sure, you can fire up Photoshop or 0:12 whatever image editing software you want to use on your computer. 0:13 But what if you want to do these transformations to images automatically 0:16 like in your web app or to an entire directory full of images? 0:20 Python to the rescue. 0:23 There's a popular and powerful Python library named Pillow for handling images. 0:25 If you've looked around on the internet for tutorials on this, 0:30 you've probably seen something named PIL or the Python imaging library referenced. 0:32 PIL was great. 0:37 But it hasn't been updated since around 2009 and doesn't support Python 3. 0:39 Luckily the community stepped in and created Pillow, 0:43 which is still under active development and supports Python 3 just fine. 0:46 Pretty much any tutorial you find for 0:50 Pill will work with Pillow too, since the two have nearly identical APIs. 0:52 I won't be talking about the differences between the two but 0:57 the Pillow docs have a section on porting from Pill to Pillow. 0:59 You'll need to install Pillow if you want to play along locally. 1:03 You'll do that with [SOUND] pip install pillow. 1:06 And you may need to install some system level libraries before you can deal 1:09 with certain image types. 1:12 This is all included in the Pillow documentation so 1:13 be sure to check the teacher's notes for a link to it. 1:16 Let's go see what we can do. 1:18 So I think this was one of the first workshops where I've used PyCharm. 1:21 We're gonna be doing all of this locally because it's not easy to do on workspaces. 1:24 So, you'll just need to install Pillow on your machine and 1:29 then you'll be able to run all this stuff. 1:33 So, like I said in the intro, Pillow is a new version of the old package, 1:35 PlL, or the Python Imaging Library. 1:40 Now because of this, even though we're using Pillow, 1:43 we're gonna be importing everything from PIL, P-I-L. 1:45 Yeah, it's confusing when you come to Pillow first if you've never used PIL. 1:49 But if you've used PIL before, this makes it super easy to upgrade your packages, 1:53 your own software, without having to change very much code. 1:57 Pillow has a lot of modules. 2:01 Let me just show you how many modules Pillow has. 2:04 So here's Pillow, Pillow's stuff. 2:07 Let's look here at reference. 2:09 And these are all of Pillow's modules. 2:12 Look at this, there's 15, 20 of them. 2:15 We're not going to cover all of those. 2:18 But, we will cover a few of them. 2:20 Mostly we're going to cover this image module and 2:23 then we'll look at image enhance and image filter as well. 2:26 So, these docs are amazing by the way, you should be sure and 2:31 look through these on your own. 2:34 So, I've got a couple of images here, I have this balloons image. 2:36 Let's see, there we go, and then I have this ribbons image as well. 2:39 I took both of these at our recent Jango girls workshop. 2:47 So, I'm gonna use these two images as ones to mess around with. 2:51 You can, of course, modify any images that you want or 2:55 that you happen to have on your own machine. 2:58 Okay, so let's get started. 3:02 So over here in my file, I'm going to do from PIL import Image. 3:05 It's a little bit weird how PIL has capital letters for the module name. 3:10 But that's not something that you come across a whole lot, but 3:16 it is an older thing so, that's probably where it comes from. 3:19 Okay, let's just start off. 3:23 I want to get the balloons image and 3:24 balloons is going to equal image.open and then I want to pass it a file name. 3:28 And in this case, that is ballons.jpg. 3:34 Let's do the same thing for the ribbons image. 3:37 And there we go, okay, so we have these two images. 3:44 So what can we do with them? 3:47 Well probably the easiest thing we can do is just show them. 3:48 Right? I mean we have them both open. 3:53 Pill, pillow, is now paying attention to them. 3:55 We can do transforms and adjustments and all that stuff. 3:58 We're gonna get to that later. 4:01 For now though, let's just see them. 4:02 So let's call balloons.show and let's call ribbons.show. 4:04 All right? 4:09 And now when we run this Yep, run workshop. 4:10 It just opens up two temporary images from off of our theme. 4:19 Now how do I know they're temporary images? 4:25 Both my images are jpegs, these are both bitmaps. 4:27 And, if we could view where this path was it would be 4:30 a big long path back to our temp folder. 4:33 So, we call that show, that shows our stuff. 4:36 We're going to be using .show a lot in this, so just get used to that. 4:39 I'll cover how to save images later, so don't worry about that for right now. 4:43 Okay, let's take that out, and let's just do a small transform. 4:48 Let's just rotate the balloons image. 4:53 Okay so we call the rotate method and 4:56 let's rotate it by 45 degrees and then call show. 4:59 So this is gonna be kind of our standard work flow for the entire workshop. 5:02 And there it is you can see it's been rotated by 45 degrees. 5:08 All right, cool. 5:11 Again, that doesn't change the one on disc. 5:14 That just makes a temporary one and shows that to me. 5:17 Okay, so what else can we do? 5:21 Well, we can also get the size, the mode, other information about the Images. 5:22 So let's try that. 5:30 Let's try ribbons.size and ribbons.mode. 5:31 And we're just gonna print those. 5:36 Let's run that and we can see the size, it's 2592 pixels wide by 3888 tall. 5:39 And the mode is RGB, which makes sense, it's a digital photo off of a camera, 5:48 it's not gonna be in like CMYK or anything like that, so RGB makes a lot of sense. 5:52 It's also a jpg, jpg's are usually RPG. 5:56 RGB, not RPG, totally different thing there. 6:00
You need to sign up for Treehouse in order to download course files.Sign up