[MUSIC PLAYING] SPEAKER: Well, Hello one and all, and welcome to our short on Pillow, which is a library for manipulating images within Python. So I have here a file, an image called in.jpeg And this is the Great Wave Off Kanagawa by the artist Hokusai. Now if you're familiar with this image, it should not be upside down like this. And I really want to flip it over so I can see exactly what this image holds. Well, thankfully I can use something like Python with the Pillow library to do just that, to manipulate this image and rotate it so it's going to be right side up instead. So let's go ahead and write a program to do just that. I will type down my terminal code image.py. And here, I want to give myself, likely, a main function to write my program in, and I'll call main down below. Now if I want to use the Pillow library for manipulating images in Python, I can access it by doing import PIL. Now really, I want access to some particular class inside of this Pillow library, one called image. More on classes in a future week on object-oriented programming. But for now, think of a class as a bit like a template, some way of representing information. In this case, an image. So I could access some class that Pillow provides me by typing from PIL import Image, capital I. So the Pillow library gives me access to something called an Image that I can use to represent and manipulate images that I might have on my own file system, on my computer. So using this Image class, I can actually go ahead and try to open up an image, and I can do so using a method of the Image class, one called open. I can access it using Image.Open, and I'll give as input to open, in this case, the name of the file I'm hoping to open now in Python. Very similar in spirit to me clicking on this file on the left-hand side in .jpeg and seeing what's inside of it, open goes ahead and opens that image for use within my Python program here. Now open, this method here, returns to me an image object. So I'll go ahead and use this img variable to refer to my image that I have now opened using the Pillow library. And similar to other files we open in Python, it's good practice to close them when we're done with them. So as a test here, I've opened up my image. But I should now close it. So I'll type img.close. This method goes ahead and closes my image after I have opened it. Now while we're here, we might have seen before that it's maybe not best practice to get in the habit of defining explicitly when we're going to open and close our images, in part because we'll often forget to close things later on. So why don't I do this instead? I'll go ahead and use with, with image.open as img. And as long as I'm indented within this with block, I'm now able in this case to see my image being opened. And I can run all kinds of functions on it as well. So as long as we're indented, we have access to this variable called img, which is, in this case, the image I've opened called in.jpeg. So what could we do with it? Well, one thing we could do is get a sense for the size and the format of this image, thanks to various attributes of this image object. So I could, for instance, try printing img.size. And this actually gives me access to the size of the image in pixels. I could also print-- I could also print img.format, and this, for me, will show me the format of the image, like what file type it is, if I'm not sure initially. So I'll go ahead and run Python of image.py, and hopefully, fingers crossed, we'll see some information down below. So it seems like this image in .jpeg, its size is 1,052 by 720. So maybe 1,052 pixels across and 720 pixels up, or vertical. And this image's format seems to be a jpeg, which makes sense, because it has the .jpeg file extension here. So we can access to various properties of this image. We can also manipulate it using Pillow as well. One thing we could do is very simply rotate it. So here, if we look back at our image in .jpeg, we'll see it is upside down. So we maybe want to rotate it another 180 degrees to put it right side up. Well in image.py, I can do just that. I could say something like img.rotate. And I'll add in the degrees I want to rotate this image. In this case, 180 to put it back right side up. I'll then store this updated image in the img variable. And now, if I want to, let's say, save this updated image, I can do so by calling img.save and giving some particular file name, like out.jpeg, just like this. So to be clear, I'm opening in.jpeg, rotating it, thanks to Pillow with this rotate method, and then saving it, again thanks to Pillow, with this save method. I'll go ahead and run Python of image.py and we'll see nothing in the terminal. But you might have seen over here I now have a file, out.jpeg. I'll open it up and we'll see this is that image, but now right side up. Well, what else could we do? One thing we could do is apply filters to images. Have a bit of fun here. We could rotate it certainly to fix the image, but along the way, maybe apply a fun filter to learn more about the image. Well, one filter we can apply is a blur filter to make it look a little bit more blurry. And to access image filters in the Pillow library, I can do this. From PIL, import imageFilter. So imageFilter here is a class that I can then use to apply various image filters. And I can actually apply them to my image by using a method, filter. So I can type img.filter, and then give as input the filter I want to apply. And by convention here, if I want to get the blur filter, I can type imageFilter.BLUR in all caps. I'll now save this image. Well, I will update, let's say, my image in the img variable and save that updated image using the Save method later on. I'll type Python of image.py and let's see the updated version of out.jpeg. Seems a little bit more blurry when compared to in.jpeg. So that filter has been applied here. What else could we do? Well, one other fun filter is one called find edges that quickly finds the edges of the image and highlights them for us. I can access that by using imageFilter.FIND_EDGES. And now if I run Python of image.py, what should I see but again, that image rotated 180 degrees and now a filter that finds the edges of the image for me. So much more you can do with Pillow. But we've seen here how to open images, how to save them, how to rotate them, and how to apply filters. We're excited to see what you'll create with Pillow.