1 00:00:00,000 --> 00:00:02,445 [MUSIC PLAYING] 2 00:00:02,445 --> 00:00:05,870 3 00:00:05,870 --> 00:00:09,320 SPEAKER: Well, Hello one and all, and welcome to our short on Pillow, 4 00:00:09,320 --> 00:00:13,530 which is a library for manipulating images within Python. 5 00:00:13,530 --> 00:00:20,300 So I have here a file, an image called in.jpeg And this is the Great Wave Off 6 00:00:20,300 --> 00:00:22,610 Kanagawa by the artist Hokusai. 7 00:00:22,610 --> 00:00:27,390 Now if you're familiar with this image, it should not be upside down like this. 8 00:00:27,390 --> 00:00:32,580 And I really want to flip it over so I can see exactly what this image holds. 9 00:00:32,580 --> 00:00:37,220 Well, thankfully I can use something like Python with the Pillow library 10 00:00:37,220 --> 00:00:39,680 to do just that, to manipulate this image 11 00:00:39,680 --> 00:00:43,280 and rotate it so it's going to be right side up instead. 12 00:00:43,280 --> 00:00:45,720 So let's go ahead and write a program to do just that. 13 00:00:45,720 --> 00:00:50,300 I will type down my terminal code image.py. 14 00:00:50,300 --> 00:00:55,370 And here, I want to give myself, likely, a main function to write my program in, 15 00:00:55,370 --> 00:00:58,130 and I'll call main down below. 16 00:00:58,130 --> 00:01:03,350 Now if I want to use the Pillow library for manipulating images in Python, 17 00:01:03,350 --> 00:01:07,680 I can access it by doing import PIL. 18 00:01:07,680 --> 00:01:13,380 Now really, I want access to some particular class inside of this Pillow 19 00:01:13,380 --> 00:01:16,090 library, one called image. 20 00:01:16,090 --> 00:01:20,350 More on classes in a future week on object-oriented programming. 21 00:01:20,350 --> 00:01:23,220 But for now, think of a class as a bit like a template, 22 00:01:23,220 --> 00:01:25,200 some way of representing information. 23 00:01:25,200 --> 00:01:27,130 In this case, an image. 24 00:01:27,130 --> 00:01:31,110 So I could access some class that Pillow provides me 25 00:01:31,110 --> 00:01:36,210 by typing from PIL import Image, capital I. 26 00:01:36,210 --> 00:01:41,010 So the Pillow library gives me access to something called an Image that I 27 00:01:41,010 --> 00:01:45,960 can use to represent and manipulate images that I might have on my own file 28 00:01:45,960 --> 00:01:47,490 system, on my computer. 29 00:01:47,490 --> 00:01:51,510 So using this Image class, I can actually 30 00:01:51,510 --> 00:01:53,910 go ahead and try to open up an image, and I 31 00:01:53,910 --> 00:01:58,840 can do so using a method of the Image class, one called open. 32 00:01:58,840 --> 00:02:01,410 I can access it using Image.Open, and I'll 33 00:02:01,410 --> 00:02:05,250 give as input to open, in this case, the name of the file 34 00:02:05,250 --> 00:02:08,199 I'm hoping to open now in Python. 35 00:02:08,199 --> 00:02:12,735 Very similar in spirit to me clicking on this file on the left-hand side in .jpeg 36 00:02:12,735 --> 00:02:17,520 and seeing what's inside of it, open goes ahead and opens that image for use 37 00:02:17,520 --> 00:02:19,830 within my Python program here. 38 00:02:19,830 --> 00:02:24,550 Now open, this method here, returns to me an image object. 39 00:02:24,550 --> 00:02:29,130 So I'll go ahead and use this img variable to refer to my image 40 00:02:29,130 --> 00:02:32,520 that I have now opened using the Pillow library. 41 00:02:32,520 --> 00:02:36,010 And similar to other files we open in Python, 42 00:02:36,010 --> 00:02:38,740 it's good practice to close them when we're done with them. 43 00:02:38,740 --> 00:02:41,640 So as a test here, I've opened up my image. 44 00:02:41,640 --> 00:02:43,150 But I should now close it. 45 00:02:43,150 --> 00:02:45,490 So I'll type img.close. 46 00:02:45,490 --> 00:02:50,850 This method goes ahead and closes my image after I have opened it. 47 00:02:50,850 --> 00:02:56,010 Now while we're here, we might have seen before that it's maybe not best practice 48 00:02:56,010 --> 00:02:58,770 to get in the habit of defining explicitly 49 00:02:58,770 --> 00:03:01,230 when we're going to open and close our images, 50 00:03:01,230 --> 00:03:04,570 in part because we'll often forget to close things later on. 51 00:03:04,570 --> 00:03:06,330 So why don't I do this instead? 52 00:03:06,330 --> 00:03:12,540 I'll go ahead and use with, with image.open as img. 53 00:03:12,540 --> 00:03:16,260 And as long as I'm indented within this with block, 54 00:03:16,260 --> 00:03:20,530 I'm now able in this case to see my image being opened. 55 00:03:20,530 --> 00:03:23,560 And I can run all kinds of functions on it as well. 56 00:03:23,560 --> 00:03:25,410 So as long as we're indented, we have access 57 00:03:25,410 --> 00:03:29,260 to this variable called img, which is, in this case, 58 00:03:29,260 --> 00:03:31,420 the image I've opened called in.jpeg. 59 00:03:31,420 --> 00:03:32,850 So what could we do with it? 60 00:03:32,850 --> 00:03:35,700 Well, one thing we could do is get a sense for the size 61 00:03:35,700 --> 00:03:40,840 and the format of this image, thanks to various attributes of this image object. 62 00:03:40,840 --> 00:03:46,420 So I could, for instance, try printing img.size. 63 00:03:46,420 --> 00:03:50,950 And this actually gives me access to the size of the image in pixels. 64 00:03:50,950 --> 00:03:52,020 I could also print-- 65 00:03:52,020 --> 00:03:56,680 I could also print img.format, and this, for me, 66 00:03:56,680 --> 00:04:00,150 will show me the format of the image, like what file type it 67 00:04:00,150 --> 00:04:02,490 is, if I'm not sure initially. 68 00:04:02,490 --> 00:04:05,640 So I'll go ahead and run Python of image.py, 69 00:04:05,640 --> 00:04:10,240 and hopefully, fingers crossed, we'll see some information down below. 70 00:04:10,240 --> 00:04:17,790 So it seems like this image in .jpeg, its size is 1,052 by 720. 71 00:04:17,790 --> 00:04:24,130 So maybe 1,052 pixels across and 720 pixels up, or vertical. 72 00:04:24,130 --> 00:04:28,530 And this image's format seems to be a jpeg, which makes sense, 73 00:04:28,530 --> 00:04:31,860 because it has the .jpeg file extension here. 74 00:04:31,860 --> 00:04:35,850 So we can access to various properties of this image. 75 00:04:35,850 --> 00:04:39,420 We can also manipulate it using Pillow as well. 76 00:04:39,420 --> 00:04:42,310 One thing we could do is very simply rotate it. 77 00:04:42,310 --> 00:04:47,940 So here, if we look back at our image in .jpeg, we'll see it is upside down. 78 00:04:47,940 --> 00:04:54,090 So we maybe want to rotate it another 180 degrees to put it right side up. 79 00:04:54,090 --> 00:04:57,400 Well in image.py, I can do just that. 80 00:04:57,400 --> 00:05:03,822 I could say something like img.rotate. 81 00:05:03,822 --> 00:05:08,000 And I'll add in the degrees I want to rotate this image. 82 00:05:08,000 --> 00:05:11,280 In this case, 180 to put it back right side up. 83 00:05:11,280 --> 00:05:15,960 I'll then store this updated image in the img variable. 84 00:05:15,960 --> 00:05:19,970 And now, if I want to, let's say, save this updated image, 85 00:05:19,970 --> 00:05:24,980 I can do so by calling img.save and giving some particular file 86 00:05:24,980 --> 00:05:28,790 name, like out.jpeg, just like this. 87 00:05:28,790 --> 00:05:33,530 So to be clear, I'm opening in.jpeg, rotating it, thanks to Pillow 88 00:05:33,530 --> 00:05:37,100 with this rotate method, and then saving it, again thanks to Pillow, 89 00:05:37,100 --> 00:05:39,080 with this save method. 90 00:05:39,080 --> 00:05:45,180 I'll go ahead and run Python of image.py and we'll see nothing in the terminal. 91 00:05:45,180 --> 00:05:49,470 But you might have seen over here I now have a file, out.jpeg. 92 00:05:49,470 --> 00:05:54,960 I'll open it up and we'll see this is that image, but now right side up. 93 00:05:54,960 --> 00:05:56,160 Well, what else could we do? 94 00:05:56,160 --> 00:05:58,710 One thing we could do is apply filters to images. 95 00:05:58,710 --> 00:05:59,950 Have a bit of fun here. 96 00:05:59,950 --> 00:06:03,650 We could rotate it certainly to fix the image, but along the way, 97 00:06:03,650 --> 00:06:07,610 maybe apply a fun filter to learn more about the image. 98 00:06:07,610 --> 00:06:11,450 Well, one filter we can apply is a blur filter 99 00:06:11,450 --> 00:06:13,640 to make it look a little bit more blurry. 100 00:06:13,640 --> 00:06:18,470 And to access image filters in the Pillow library, I can do this. 101 00:06:18,470 --> 00:06:23,070 From PIL, import imageFilter. 102 00:06:23,070 --> 00:06:25,460 So imageFilter here is a class that I can then 103 00:06:25,460 --> 00:06:28,370 use to apply various image filters. 104 00:06:28,370 --> 00:06:33,030 And I can actually apply them to my image by using a method, filter. 105 00:06:33,030 --> 00:06:38,970 So I can type img.filter, and then give as input the filter I want to apply. 106 00:06:38,970 --> 00:06:41,630 And by convention here, if I want to get the blur filter, 107 00:06:41,630 --> 00:06:46,580 I can type imageFilter.BLUR in all caps. 108 00:06:46,580 --> 00:06:49,340 I'll now save this image. 109 00:06:49,340 --> 00:06:53,810 Well, I will update, let's say, my image in the img variable 110 00:06:53,810 --> 00:06:57,950 and save that updated image using the Save method later on. 111 00:06:57,950 --> 00:07:04,170 I'll type Python of image.py and let's see the updated version of out.jpeg. 112 00:07:04,170 --> 00:07:08,380 Seems a little bit more blurry when compared to in.jpeg. 113 00:07:08,380 --> 00:07:10,330 So that filter has been applied here. 114 00:07:10,330 --> 00:07:11,280 What else could we do? 115 00:07:11,280 --> 00:07:15,090 Well, one other fun filter is one called find 116 00:07:15,090 --> 00:07:19,750 edges that quickly finds the edges of the image and highlights them for us. 117 00:07:19,750 --> 00:07:24,160 I can access that by using imageFilter.FIND_EDGES. 118 00:07:24,160 --> 00:07:31,050 And now if I run Python of image.py, what should I see but again, 119 00:07:31,050 --> 00:07:35,070 that image rotated 180 degrees and now a filter that 120 00:07:35,070 --> 00:07:37,950 finds the edges of the image for me. 121 00:07:37,950 --> 00:07:40,690 So much more you can do with Pillow. 122 00:07:40,690 --> 00:07:45,210 But we've seen here how to open images, how to save them, how to rotate them, 123 00:07:45,210 --> 00:07:47,050 and how to apply filters. 124 00:07:47,050 --> 00:07:50,360 We're excited to see what you'll create with Pillow. 125 00:07:50,360 --> 00:07:52,000