1 00:00:00,348 --> 00:00:04,280 So, where Pillow comes in super handy 2 00:00:04,280 --> 00:00:08,300 is when you use it to apply transformations to images, right? 3 00:00:08,300 --> 00:00:11,300 For example, maybe you want all of your images to be black and 4 00:00:11,300 --> 00:00:12,120 white instead of color. 5 00:00:12,120 --> 00:00:14,830 You don't want any color images, you just want black and white images. 6 00:00:14,830 --> 00:00:17,970 So we can do that by changing the mode of our image. 7 00:00:17,970 --> 00:00:22,176 We talked before how ribbons' mode was RGB, so. 8 00:00:22,176 --> 00:00:26,664 In Pillow, then images, 9 00:00:26,664 --> 00:00:34,220 images can be in multiple different modes. 10 00:00:34,220 --> 00:00:38,460 And if we look here at the modes, we can see there's mode one and mode L, 11 00:00:38,460 --> 00:00:42,650 which are both black and white, one is 1-bit pixels, the other is 8-bit pixels. 12 00:00:42,650 --> 00:00:49,640 We have p which is 8 bit mapped to a color palette RGB, RGBA, CMYK and so on. 13 00:00:49,640 --> 00:00:52,240 So, depending on the kinds of images that you are wanting to work with, 14 00:00:52,240 --> 00:00:55,420 you may be using different types of modes. 15 00:00:55,420 --> 00:01:01,400 So let's see about turning our balloons image, into black and white. 16 00:01:01,400 --> 00:01:03,780 So we do that by converting the mode. 17 00:01:03,780 --> 00:01:05,940 We're gonna turn the mode to capital L. 18 00:01:07,080 --> 00:01:08,290 And then we're gonna call show. 19 00:01:09,700 --> 00:01:11,180 So now the original was RGB. 20 00:01:11,180 --> 00:01:15,110 Here we're going to change it to L, which is an 8 bit black and white. 21 00:01:15,110 --> 00:01:16,600 And this is what we get out of it. 22 00:01:17,910 --> 00:01:23,350 So not a bad looking image, just converted straight to black and white. 23 00:01:23,350 --> 00:01:26,470 I mentioned we can turn it to one, and 24 00:01:26,470 --> 00:01:28,530 this will produce a one bit black and white. 25 00:01:28,530 --> 00:01:30,140 And you will see the difference. 26 00:01:31,150 --> 00:01:32,990 This is one bit black and white. 27 00:01:32,990 --> 00:01:35,580 Not nearly as good. 28 00:01:35,580 --> 00:01:38,860 But the one bit often helps to preserve luminosity a little bit better, 29 00:01:38,860 --> 00:01:43,170 so you might end up using that in different kinds of applications. 30 00:01:43,170 --> 00:01:46,800 So for us, though, I think we'd want L versus 1. 31 00:01:46,800 --> 00:01:50,090 Alright, so that's changing color. 32 00:01:50,090 --> 00:01:53,430 What about resizing or cropping images? 33 00:01:53,430 --> 00:01:55,100 Those are actually two different things. 34 00:01:55,100 --> 00:01:59,010 A crop only contains part of an image while a resize is the entire image, but 35 00:01:59,010 --> 00:02:01,760 been, you know, shrunk down or blown up. 36 00:02:01,760 --> 00:02:02,740 So let's try them out one at a time. 37 00:02:02,740 --> 00:02:04,690 Let's try out resizing first. 38 00:02:04,690 --> 00:02:09,090 So to resize an image we get to use the resize method. 39 00:02:09,090 --> 00:02:10,690 Surprising huh? 40 00:02:10,690 --> 00:02:12,480 Resize takes a pair or 41 00:02:12,480 --> 00:02:16,860 a tuple with just two values in it of the new size that you want the image to be. 42 00:02:16,860 --> 00:02:20,400 So let's do ribbons.resize. 43 00:02:20,400 --> 00:02:22,520 And let's make it say 500 by 500. 44 00:02:22,520 --> 00:02:28,890 And I didn't mean to type save, I meant to type show. 45 00:02:28,890 --> 00:02:33,610 All right, so let's resize ribbons to 500 by 500. 46 00:02:33,610 --> 00:02:35,170 So here it is. 47 00:02:35,170 --> 00:02:37,670 And you can't tell a whole lot here. 48 00:02:37,670 --> 00:02:39,060 But it's definitely squashed. 49 00:02:39,060 --> 00:02:42,990 It should not be this wide, if it's only gonna be that tall. 50 00:02:42,990 --> 00:02:46,360 So, we could make this larger if we needed to, of course, but 51 00:02:46,360 --> 00:02:48,410 then we blow up the image. 52 00:02:48,410 --> 00:02:52,770 Resize does take another set of values or another value, rather, which 53 00:02:52,770 --> 00:02:57,360 is the resampling filter, so like nearest neighbor, by cubic, stuff like that. 54 00:02:57,360 --> 00:02:59,680 I'll leave that for you to experiment with. 55 00:02:59,680 --> 00:03:04,460 We didn't take the original aspect ratio into consideration here when we did this. 56 00:03:04,460 --> 00:03:09,568 Let's do that, so we can come back here and we can say ribbons.width 57 00:03:09,568 --> 00:03:14,340 divided by two, and we can say ribbons.height divided by two. 58 00:03:14,340 --> 00:03:16,590 And let's run that. 59 00:03:16,590 --> 00:03:20,970 And, that did not run because it expected an integer and it got a float. 60 00:03:20,970 --> 00:03:25,245 So, let's do this as integer division with the two slashes. 61 00:03:26,780 --> 00:03:27,856 Run that. 62 00:03:27,856 --> 00:03:30,650 And now we have an image that, it doesn't necessarily look smaller, but 63 00:03:30,650 --> 00:03:32,120 it is a smaller image. 64 00:03:33,130 --> 00:03:38,080 We go to Inspector, let me see that it's now only 1296 x 65 00:03:38,080 --> 00:03:42,590 1944 instead of the 2000 something that it was before. 66 00:03:43,610 --> 00:03:46,410 Alright, cool, so that's resizing. 67 00:03:46,410 --> 00:03:51,620 Let's try one more that's along the lines of resizing, let's try doing a thumbnail. 68 00:03:51,620 --> 00:03:55,230 Now thumbnailing in Pillow is a little bit different then you might think. 69 00:03:55,230 --> 00:04:00,090 You might think, oh I just resized down to a super small size. 70 00:04:00,090 --> 00:04:06,090 Thumbnailing actually gives us an image that fits within a certain square. 71 00:04:06,090 --> 00:04:08,460 So we need to resize it. 72 00:04:08,460 --> 00:04:11,300 One of the problems though is that resize, it does not give you a new image. 73 00:04:11,300 --> 00:04:14,910 These other ones we've been calling these dot show beginning this new image 74 00:04:14,910 --> 00:04:19,110 out of temp, and we don't get that with thumbnail. 75 00:04:19,110 --> 00:04:24,480 Thumbnail changes our image object in place it applies a mutation, 76 00:04:24,480 --> 00:04:26,130 right, to a mutable type. 77 00:04:26,130 --> 00:04:30,020 So let's say b2 is balloons.copy. 78 00:04:30,020 --> 00:04:33,850 Copy is a method that lets us copy an image. 79 00:04:33,850 --> 00:04:36,600 And then let's say b2.thumbnail. 80 00:04:36,600 --> 00:04:40,800 And then we specify a square or a box. 81 00:04:40,800 --> 00:04:41,790 It doesn't have to be a square. 82 00:04:41,790 --> 00:04:45,460 When you specify a box that we want the thumbnail to fit into, so 83 00:04:45,460 --> 00:04:49,880 let's say it should be no bigger than 500 pixels in any direction. 84 00:04:50,920 --> 00:04:52,824 And then let's say b2.show. 85 00:04:54,300 --> 00:04:59,470 So let's run that and now we've got this nice little image here that 86 00:04:59,470 --> 00:05:05,940 is 500 by 333 so it fits inside of our 500 pixel box and it's been nicely resized.