Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Cropping and Rotating4:15 with Kenneth Love
Sometimes we need to turn images around or cut them down in size.
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.
Okay, so that's thumbnailing.
It's handy, it makes it great for working inside of design constraints.
But now, what if I need to do cropping,
what if I need to have only part of an image?
All right, so cropping gives us back, like I just said, part of an image,
and it does that based on a couple of four coordinates that we give it.
So I got some coordinates from balloons.jpg earlier so
I'd know where I wanted to do the crops.
So I'm actually gonna make this a variable called box cuz we're gonna use this
a couple of times.
And it's gonna be (22, 324, 826, 846).
Now this is gonna depend very, very much on whatever image you're working with.
You can use most image editing software like Photoshop or MS Paint or
whatever to find pixel coordinates.
So you just gotta find those on your own.
Or this is the kind of thing that you end up getting from your users with some sort
a box on the image, you'll find out where the edges are and then crop to that.
So, let's try this, let's do balloons.crop(box).show And
we run that, and now we've got just part of our balloon image.
Just the part that had these green and gold balloons in it.
So that's cool, we're able to get just a little bit there, okay?
If you're wondering, this is the left side, the top side,
the right side, and the bottom side.
So that goes left, top, right, bottom.
Clockwise around the image.
So you can always specify where those edges are.
So easy enough on that.
What if you want to rotate an image, right?
Maybe it's a vertical image where it should be horizontal or vice versa.
So just like with cropping and resizing, there is a method for it.
So let's try balloons.rotate(90).show.
Isn't it kind of awesome how all these methods are like named what you would
expect them to be?
So if we rotate our balloons by 90, we get this.
So it rotated it counter-clockwise.
If you wanted it to go the other direction, you'd put in, like, -90 or
you could go all the way around the circle and go to 270.
There is another important argument to rotate.
Rotate, like resize, takes a resampling filter.
But the more important one to know about is expand.
If you set expand to true, the image will be resized to show the entire rotation.
If you leave it blank or set it to false, then it's rotated but
it's left to the same dimension.
So like that one, it's swapped around.
We could still see the whole image, but
it's because it still has the same dimensions.
If we change this to, say, 45.
I feel like we did this one before.
It kept the old dimensions and we get these black boxes,
these little black segments on the side.
If we set expand=True, and
we run this again, now we've got this thing, and
we can see all four corners of our image, even though it's been rotated.
So, we've been making all of these adjusted images.
We haven't been doing anything with them.
How do you save images to disk?
Well, believe it or not, it's a method called save.
Let's just rotate this back to 90.
And instead of calling .show, I'm gonna call .save.
Actually let's do -90.
And I'm gonna call this balloons2.jpg.
And it should save to the same directory, so let's run that.
And now we've got balloons2 and if we pull that up and shrink things down,
yep, it is a negative 90 degree rotated version of balloons.
Cool, so it did what we wanted.
You can override the format.
You can specify a format equals and I think it's like png or
something like that.
And there's stuff in the docs for how to do that.
I"ll be sure and link to it in the teacher's notes.
But most of the time you're gonna end up saving it back as
the same kind of file that it is, so not something you really have to worry about.
You need to sign up for Treehouse in order to download course files.Sign up