Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Enhance and Filter5:02 with Kenneth Love
Now that we're comfortable manipulating images, let's look at the `ImageEnhance` and `ImageFilter` modules to see about applying some enhancements and filters to our images for more advanced effects.
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.
So far, we've only talked about the image module. 0:00 And the image module lets us do quite a bit of stuff, but 0:03 maybe we want to do some enhancements or we want to do some filters. 0:06 So, if those are the case, we should look at how to do enhance. 0:10 ImageEnhance lets us do things like sharpen an image, change the color, 0:15 change the contrast, stuff like that and 0:19 the filter module lets us do things like blurring or EMBOSSING, Gaussian blurring. 0:21 They give us a little bit of Photshopy abilities inside Pillow. 0:26 They're not necessarily as powerful as Photoshop, so 0:31 don't think they are a replacement for it. 0:35 But they're a good way to apply small effects to images. 0:36 So we need to go ahead and import them. 0:40 So from_PIL import. 0:42 Let's just import both of them. 0:43 ImageEnhance, ImageFilter. 0:45 All right. 0:51 So, first let's do an ImageEnhance. 0:53 So I think that our balloons could be more interesting if they were desaturated, or 0:55 at least partially desaturated. 1:00 Put them closer to gray. 1:02 So we need to instantiate a color enhancer for this image. 1:03 This is a little bit of a weird interface, 1:08 but it makes a lot of sense once you get used to it. 1:09 So we'll say balloons_enhancer is an ImageEnhance.Color. 1:12 And then the image that we want it to work on is the balloons image. 1:22 And now we can apply the enhancements, so we can say balloons_enhancer.enhance. 1:27 And then, how much do we want to do it? 1:35 What's our factor? 1:37 So if we do 0.0, or just 0, that'll make it all the way grey. 1:38 If we do it at 1, it'll be the regular full color image that it already is. 1:42 So let's go half way saturated, let's say 0.5, then we'll say to show this. 1:47 And we get a nicely desaturated image. 1:55 Our greens, our golds are no where near as bright as they were before. 1:58 We've got a bit of a maybe a bit of an Instagram, hipstery feel to it. 2:01 You could, of course, create your own Instagram with this kind of work. 2:05 All right, there are some other enhancements that you might want to check 2:09 out inside the enhance module here. 2:12 That's the only one I'm going to show you. 2:15 But like contrast would let you, again, move it toward gray, brightness would 2:16 let you move it up or down, and sharpness would let you sharpen it. 2:20 Notice that sharpness goes up to 2.0, 2:25 whereas the other ones only go between 0 and 1.0. 2:27 Good idea to play with these a little bit. 2:30 All right so now let's talk about filters. 2:33 So most of these filters are kind of obvious, 2:36 find edges, smooth, stuff like that. 2:39 Let's play with this Gaussian blur cuz this one's fun. 2:41 So we're gonna take that out. 2:44 And yeah, so like I said, it's kind of like a code driven Photoshop. 2:48 So let's take our ribbons image and let's blur that. 2:52 So we'll say ribbons.filter. 2:54 Filter is a method on the image class and 2:57 there's all sorts of filter stuff in there. 3:01 But the easiest way to do it is to use the image filters along with it. 3:03 So we'll say ImageFilter.GaussianBlur and then we get to specify a radius. 3:07 And let's say the radius is 10 pixels and then show. 3:12 So we blur ever pixel by 10 pixels. 3:16 And when we run that it takes a little bit of time to run that. 3:20 But you can see now that we have a very nicely blurred image. 3:23 So, cool. 3:27 That's kind of neat. 3:28 All right. 3:30 So that's how we use the Gaussian blur one. 3:31 The rest of them all work the same way. 3:34 If you look down through here there's like unsharp mask there's other stuff. 3:35 These are all going to work. 3:39 The same way. 3:42 You'll just specify what things you want to put in. 3:42 But then, these up here are some predefined ones. 3:45 So let's check out, we'll say the EMBOSS one. 3:48 So we'll go back over here, and actually I want to do this one to the balloons. 3:52 I think that might be a more interesting thing. 3:56 And so instead of this GaussianBlur(radius=10), 4:00 we're just going to do EMBOSS. 4:04 All in caps. 4:06 Like an EMBOSS, right? 4:08 And that gives us this amazing [LAUGH] and super useful image. 4:10 Actually I'm not sure this is useful at all. 4:16 And then it shows you where the edges are. 4:18 That can be really handy depending on what you're trying to do. 4:19 Maybe you're trying to detect faces or 4:21 something like that, letters, words in a sign. 4:23 I don't know when this would be useful but if you ever need automated embossing, 4:27 then we definitely have that available. 4:31 So Pillow let's us do quite a bit with images. 4:34 I did not mean to push tha button. 4:37 Pillow lets us do quite a bit with images. 4:40 Its really handy and its definitely a tool that I'm happy to have in my back pocket. 4:42 Pillow gives you a ton of power over images. 4:47 I've used it to create thumbnails, rotate images back to the proper 4:50 orientation that they are rotated in the camera, and 4:53 do some interesting color effects in the past. 4:56 If you want to do more with image formats, check out the movie.pie library too. 4:58
You need to sign up for Treehouse in order to download course files.Sign up