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