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 0:00 crop it, so if you need to work with part of an image. 0:05 So to select a piece of an image, you actually use the crop method, 0:08 which makes sense. 0:12 You're just getting a piece of the image into a new variable. 0:13 So let's take our crop before and we're gonna put it into a new variable. 0:17 We're gonna say a region. 0:21 So balloons.crop(box). 0:23 Okay, now if we did a show on this, we'd get to see the piece of the image that we 0:27 just selected, like what we had before. 0:30 Let's do something else to it first, though. 0:32 So let's reassign region here, and we'll do convert. 0:36 And we'll set the mode equal to L, again. 0:40 And then let's make a copy of our balloons, so 0:45 that we don't modify the original one. 0:50 And then on balloons2, we're going to use a method called paste. 0:53 And paste let's us put stuff in. 0:57 So region is the stuff we want to put in, and then we want to put it back into box. 0:59 Okay. 1:04 So the box we put it back into, we can specify a tuple of four coordinates here. 1:04 It has to be the same dimensions as the thing as that we're putting in, 1:10 and it has to be within the bounds of the image. 1:16 So you can't put it into where it overlaps an edge and 1:19 would have pixels outside of the original image. 1:22 So it's a little bit finicky, but it's not that bad. 1:25 And let's do .show() All right. 1:30 And so if we do that, now you can see there is this gray 1:35 box right here where before we had colorful balloons. 1:38 So this could be an interesting thing to do for if you need to apply a logo or 1:42 a watermark. 1:47 If you want to do that with a watermark, you probably want it to be transparent. 1:49 You're gonna wanna look at the composite, or 1:53 I think it's called alpha composite methods. 1:55 Those are gonna be your friends there for that part. 1:57 So, let's take this part out. 2:02 Actually, let's leave this part in. 2:06 And what we're gonna do is we're gonna change this here. 2:07 So instead of converting it, there's a method called transpose. 2:09 And transpose lets us rotate or flip parts of an image. 2:15 So, let's take this, we're gonna transpose it, and we'll say, Image.FLIP_TOP_BOTTOM. 2:21 And we're gonna do that into balloons, too, okay, so now if we run this, 2:28 we get a weird little glitchy effect here, where this part here has been 2:33 flipped over from top to bottom from where it should be, you see, like this edge 2:38 here should really be matching up with this, and stuff like that. 2:42 Again, this is the kind of thing like your users might end up wanting to 2:45 have a button that flips an image or rotates an image some way, and 2:48 you can do that through transpose. 2:52 Transpose only works with 90 degree steps though, so if you need it to go 52 2:53 degrees you're gonna have to use rotate like we used earlier and not transpose. 2:59 Transpose only does 90s. 3:04
You need to sign up for Treehouse in order to download course files.Sign up