Transforming Images5:06 with Kenneth Love
Now that we can open and show images, we should start looking at how to transform them.
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, where Pillow comes in super handy 0:00 is when you use it to apply transformations to images, right? 0:04 For example, maybe you want all of your images to be black and 0:08 white instead of color. 0:11 You don't want any color images, you just want black and white images. 0:12 So we can do that by changing the mode of our image. 0:14 We talked before how ribbons' mode was RGB, so. 0:17 In Pillow, then images, 0:22 images can be in multiple different modes. 0:26 And if we look here at the modes, we can see there's mode one and mode L, 0:34 which are both black and white, one is 1-bit pixels, the other is 8-bit pixels. 0:38 We have p which is 8 bit mapped to a color palette RGB, RGBA, CMYK and so on. 0:42 So, depending on the kinds of images that you are wanting to work with, 0:49 you may be using different types of modes. 0:52 So let's see about turning our balloons image, into black and white. 0:55 So we do that by converting the mode. 1:01 We're gonna turn the mode to capital L. 1:03 And then we're gonna call show. 1:07 So now the original was RGB. 1:09 Here we're going to change it to L, which is an 8 bit black and white. 1:11 And this is what we get out of it. 1:15 So not a bad looking image, just converted straight to black and white. 1:17 I mentioned we can turn it to one, and 1:23 this will produce a one bit black and white. 1:26 And you will see the difference. 1:28 This is one bit black and white. 1:31 Not nearly as good. 1:32 But the one bit often helps to preserve luminosity a little bit better, 1:35 so you might end up using that in different kinds of applications. 1:38 So for us, though, I think we'd want L versus 1. 1:43 Alright, so that's changing color. 1:46 What about resizing or cropping images? 1:50 Those are actually two different things. 1:53 A crop only contains part of an image while a resize is the entire image, but 1:55 been, you know, shrunk down or blown up. 1:59 So let's try them out one at a time. 2:01 Let's try out resizing first. 2:02 So to resize an image we get to use the resize method. 2:04 Surprising huh? 2:09 Resize takes a pair or 2:10 a tuple with just two values in it of the new size that you want the image to be. 2:12 So let's do ribbons.resize. 2:16 And let's make it say 500 by 500. 2:20 And I didn't mean to type save, I meant to type show. 2:22 All right, so let's resize ribbons to 500 by 500. 2:28 So here it is. 2:33 And you can't tell a whole lot here. 2:35 But it's definitely squashed. 2:37 It should not be this wide, if it's only gonna be that tall. 2:39 So, we could make this larger if we needed to, of course, but 2:42 then we blow up the image. 2:46 Resize does take another set of values or another value, rather, which 2:48 is the resampling filter, so like nearest neighbor, by cubic, stuff like that. 2:52 I'll leave that for you to experiment with. 2:57 We didn't take the original aspect ratio into consideration here when we did this. 2:59 Let's do that, so we can come back here and we can say ribbons.width 3:04 divided by two, and we can say ribbons.height divided by two. 3:09 And let's run that. 3:14 And, that did not run because it expected an integer and it got a float. 3:16 So, let's do this as integer division with the two slashes. 3:20 Run that. 3:26 And now we have an image that, it doesn't necessarily look smaller, but 3:27 it is a smaller image. 3:30 We go to Inspector, let me see that it's now only 1296 x 3:33 1944 instead of the 2000 something that it was before. 3:38 Alright, cool, so that's resizing. 3:43 Let's try one more that's along the lines of resizing, let's try doing a thumbnail. 3:46 Now thumbnailing in Pillow is a little bit different then you might think. 3:51 You might think, oh I just resized down to a super small size. 3:55 Thumbnailing actually gives us an image that fits within a certain square. 4:00 So we need to resize it. 4:06 One of the problems though is that resize, it does not give you a new image. 4:08 These other ones we've been calling these dot show beginning this new image 4:11 out of temp, and we don't get that with thumbnail. 4:14 Thumbnail changes our image object in place it applies a mutation, 4:19 right, to a mutable type. 4:24 So let's say b2 is balloons.copy. 4:26 Copy is a method that lets us copy an image. 4:30 And then let's say b2.thumbnail. 4:33 And then we specify a square or a box. 4:36 It doesn't have to be a square. 4:40 When you specify a box that we want the thumbnail to fit into, so 4:41 let's say it should be no bigger than 500 pixels in any direction. 4:45 And then let's say b2.show. 4:50 So let's run that and now we've got this nice little image here that 4:54 is 500 by 333 so it fits inside of our 500 pixel box and it's been nicely resized. 4:59
You need to sign up for Treehouse in order to download course files.Sign up