Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Combining and Transposing3:08 with Kenneth Love
Now we can merge images together and we'll look at how to apply some pre-built rotations and flips to our 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.
Sometimes you need and want to do more than just make an image grayscale or
crop it, so if you need to work with part of an image.
So to select a piece of an image, you actually use the crop method,
which makes sense.
You're just getting a piece of the image into a new variable.
So let's take our crop before and we're gonna put it into a new variable.
We're gonna say a region.
Okay, now if we did a show on this, we'd get to see the piece of the image that we
just selected, like what we had before.
Let's do something else to it first, though.
So let's reassign region here, and we'll do convert.
And we'll set the mode equal to L, again.
And then let's make a copy of our balloons, so
that we don't modify the original one.
And then on balloons2, we're going to use a method called paste.
And paste let's us put stuff in.
So region is the stuff we want to put in, and then we want to put it back into box.
So the box we put it back into, we can specify a tuple of four coordinates here.
It has to be the same dimensions as the thing as that we're putting in,
and it has to be within the bounds of the image.
So you can't put it into where it overlaps an edge and
would have pixels outside of the original image.
So it's a little bit finicky, but it's not that bad.
And let's do .show() All right.
And so if we do that, now you can see there is this gray
box right here where before we had colorful balloons.
So this could be an interesting thing to do for if you need to apply a logo or
If you want to do that with a watermark, you probably want it to be transparent.
You're gonna wanna look at the composite, or
I think it's called alpha composite methods.
Those are gonna be your friends there for that part.
So, let's take this part out.
Actually, let's leave this part in.
And what we're gonna do is we're gonna change this here.
So instead of converting it, there's a method called transpose.
And transpose lets us rotate or flip parts of an image.
So, let's take this, we're gonna transpose it, and we'll say, Image.FLIP_TOP_BOTTOM.
And we're gonna do that into balloons, too, okay, so now if we run this,
we get a weird little glitchy effect here, where this part here has been
flipped over from top to bottom from where it should be, you see, like this edge
here should really be matching up with this, and stuff like that.
Again, this is the kind of thing like your users might end up wanting to
have a button that flips an image or rotates an image some way, and
you can do that through transpose.
Transpose only works with 90 degree steps though, so if you need it to go 52
degrees you're gonna have to use rotate like we used earlier and not transpose.
Transpose only does 90s.
You need to sign up for Treehouse in order to download course files.Sign up