1 00:00:08,160 --> 00:00:10,480 You'll recall that we import 2 00:00:10,480 --> 00:00:12,805 a library using the import keyword. 3 00:00:12,805 --> 00:00:15,760 So just import PIL, and we run this. 4 00:00:15,760 --> 00:00:19,600 Documentation is a big help in learning a library. 5 00:00:19,600 --> 00:00:22,600 There exists standards that make this process easier. 6 00:00:22,600 --> 00:00:24,960 For example, most of the libraries let you 7 00:00:24,960 --> 00:00:27,825 check their version using the version attribute. 8 00:00:27,825 --> 00:00:29,370 So we go PIL. 9 00:00:29,370 --> 00:00:31,330 and then double underscore. 10 00:00:31,330 --> 00:00:34,560 So this is called dander version dander. 11 00:00:34,560 --> 00:00:39,205 We see the version of the one that I'm using is 4.2.1. 12 00:00:39,205 --> 00:00:40,960 You might be using a different version 13 00:00:40,960 --> 00:00:42,850 because we might have upgraded it. 14 00:00:42,850 --> 00:00:46,490 Let's figure out how to open an image with Pillow. 15 00:00:46,490 --> 00:00:49,555 Python provides some built-in functions to help us 16 00:00:49,555 --> 00:00:50,905 understand the functions and 17 00:00:50,905 --> 00:00:53,530 objects which are available in libraries. 18 00:00:53,530 --> 00:00:55,570 For instance, the help function 19 00:00:55,570 --> 00:00:57,160 when called on any object, 20 00:00:57,160 --> 00:01:00,310 returns the objects built-in documentation. 21 00:01:00,310 --> 00:01:03,580 Let's try it with our new library and module, PIL. 22 00:01:03,580 --> 00:01:07,915 So help PIL and this renders nicely in line 23 00:01:07,915 --> 00:01:10,870 a bit of help file that actually comes from 24 00:01:10,870 --> 00:01:14,545 the documentation of the source code itself. 25 00:01:14,545 --> 00:01:16,690 This shows us that there's a host of 26 00:01:16,690 --> 00:01:19,060 classes available to us in the module, 27 00:01:19,060 --> 00:01:21,700 as well as version information and even the file 28 00:01:21,700 --> 00:01:26,725 called dander init dander.py, 29 00:01:26,725 --> 00:01:29,300 which has the source code for the module itself. 30 00:01:29,300 --> 00:01:31,370 We can look up the source code for this 31 00:01:31,370 --> 00:01:33,575 in the Jupyter console if we wanted to. 32 00:01:33,575 --> 00:01:36,350 These documentation standards make it easy to 33 00:01:36,350 --> 00:01:40,030 poke around an unexplored a library. 34 00:01:40,030 --> 00:01:43,215 Python also has a function called dir, 35 00:01:43,215 --> 00:01:45,605 which will list the contents of an object. 36 00:01:45,605 --> 00:01:48,530 This is especially useful with modules where you 37 00:01:48,530 --> 00:01:51,650 might want to see which classes you might interact with. 38 00:01:51,650 --> 00:01:54,820 Let's list the details of the PIL module. 39 00:01:54,820 --> 00:01:57,500 So dir PIL, and we can see 40 00:01:57,500 --> 00:02:00,725 here a list comes back with a bunch of strings. 41 00:02:00,725 --> 00:02:04,010 Most of these are intended to be internal functions. 42 00:02:04,010 --> 00:02:06,635 So they've got dander before and dander after, 43 00:02:06,635 --> 00:02:08,300 and that's just double underscore, 44 00:02:08,300 --> 00:02:11,385 that's a fancy way in Python that they say. 45 00:02:11,385 --> 00:02:13,820 We see that there's a couple that, 46 00:02:13,820 --> 00:02:16,135 do not have a dander and 47 00:02:16,135 --> 00:02:19,025 are expected to be used more generally. 48 00:02:19,025 --> 00:02:20,690 At the top of this list, 49 00:02:20,690 --> 00:02:22,115 there's something called Image. 50 00:02:22,115 --> 00:02:24,365 This sounds like it could be interesting for us. 51 00:02:24,365 --> 00:02:25,970 So let's import it directly, 52 00:02:25,970 --> 00:02:27,685 and run the help command on it. 53 00:02:27,685 --> 00:02:32,590 So from PIL we'll Import image, then help Image. 54 00:02:34,310 --> 00:02:37,170 Running help on the Image tells us that 55 00:02:37,170 --> 00:02:39,875 this object is the Image class wrapper. 56 00:02:39,875 --> 00:02:42,140 We see from the top level documentation 57 00:02:42,140 --> 00:02:43,490 about the image object that 58 00:02:43,490 --> 00:02:45,560 there's hardly ever any reason 59 00:02:45,560 --> 00:02:47,765 to call the image constructor directly, 60 00:02:47,765 --> 00:02:49,430 and they suggest that we use 61 00:02:49,430 --> 00:02:51,260 the open function and that's what 62 00:02:51,260 --> 00:02:54,090 we should be using to get images. 63 00:02:54,170 --> 00:02:57,040 Let's call help on the open function 64 00:02:57,040 --> 00:02:58,540 to see what it's all about. 65 00:02:58,540 --> 00:03:00,100 Remember that since we want to 66 00:03:00,100 --> 00:03:02,004 pass in the function reference, 67 00:03:02,004 --> 00:03:03,940 and not run the function itself, 68 00:03:03,940 --> 00:03:07,060 we don't put parentheses behind the function name. 69 00:03:07,060 --> 00:03:10,330 So help Image.open, we're passing 70 00:03:10,330 --> 00:03:11,950 an object but that object is 71 00:03:11,950 --> 00:03:15,470 actually a reference to the function. 72 00:03:16,340 --> 00:03:19,295 So it looks like Image.open is 73 00:03:19,295 --> 00:03:21,280 a function that loads an image from a file, 74 00:03:21,280 --> 00:03:23,920 and returns an instance of the image class. 75 00:03:23,920 --> 00:03:25,325 Let's give it a try. 76 00:03:25,325 --> 00:03:27,160 In the read_only directory there's 77 00:03:27,160 --> 00:03:28,780 an image I've provided which is 78 00:03:28,780 --> 00:03:30,310 from our Master's of Information 79 00:03:30,310 --> 00:03:32,175 program recruitment flyer. 80 00:03:32,175 --> 00:03:34,105 Let's try and load that now. 81 00:03:34,105 --> 00:03:36,655 So file we'll make it a string, 82 00:03:36,655 --> 00:03:39,595 read_only directory and then 83 00:03:39,595 --> 00:03:43,490 MSI_recruitment.gif and we'll call 84 00:03:43,490 --> 00:03:47,045 image.open and pass it this path to the file. 85 00:03:47,045 --> 00:03:48,950 That should return to us an image object, 86 00:03:48,950 --> 00:03:51,320 which we're going to put it into the image variable. 87 00:03:51,320 --> 00:03:54,190 Let's just print out this image. 88 00:03:54,190 --> 00:03:56,250 So we see it printed a 89 00:03:56,250 --> 00:03:59,840 PIL.GifImagePlugin.GifImageFile and it gives 90 00:03:59,840 --> 00:04:01,775 us some other information there. 91 00:04:01,775 --> 00:04:04,250 So we see that this returns to us 92 00:04:04,250 --> 00:04:07,645 a kind of PIL.GifImagePlugin.GifImageFile. 93 00:04:07,645 --> 00:04:10,360 At first this might seem a bit confusing, 94 00:04:10,360 --> 00:04:11,450 because we were told by 95 00:04:11,450 --> 00:04:13,369 the docs that we should be expecting 96 00:04:13,369 --> 00:04:16,715 a PIL.Image.Image object back. 97 00:04:16,715 --> 00:04:19,925 But this is actually just object inheritance working. 98 00:04:19,925 --> 00:04:21,690 In fact, the object returned is 99 00:04:21,690 --> 00:04:24,385 both an Image and a GifImageFile. 100 00:04:24,385 --> 00:04:28,025 We can use the Python inspect module to see this, 101 00:04:28,025 --> 00:04:31,640 as the getmro function will return a list of all 102 00:04:31,640 --> 00:04:32,870 of the classes that are being 103 00:04:32,870 --> 00:04:35,100 inherited by a given object. 104 00:04:35,100 --> 00:04:38,405 Let's give it a try. So we'll import inspect. 105 00:04:38,405 --> 00:04:40,120 Now this is not a third party library, 106 00:04:40,120 --> 00:04:41,825 it comes with Python. 107 00:04:41,825 --> 00:04:43,640 Then we'll write a function. 108 00:04:43,640 --> 00:04:46,220 We'll just print the type of the image. 109 00:04:46,220 --> 00:04:50,520 So a type will tell us the type of the image, 110 00:04:51,400 --> 00:04:54,605 but we want to convert that to a string. 111 00:04:54,605 --> 00:04:57,725 But then we're going to call inspect.getmro 112 00:04:57,725 --> 00:04:58,790 and pass it the type 113 00:04:58,790 --> 00:05:00,440 of the image and see 114 00:05:00,440 --> 00:05:03,875 what that inheritance chain looks like. 115 00:05:03,875 --> 00:05:06,845 So we'd see the result is 116 00:05:06,845 --> 00:05:10,205 a tuple that's returned to us which is actually 117 00:05:10,205 --> 00:05:11,990 all of the different objects that are 118 00:05:11,990 --> 00:05:13,860 being inherited from here with 119 00:05:13,860 --> 00:05:16,690 GifImagePlugin at the very 120 00:05:16,690 --> 00:05:19,420 usually bottom we would call this of the inheritance, 121 00:05:19,420 --> 00:05:21,380 the most specific version 122 00:05:21,380 --> 00:05:23,795 up to an ImageFile up to an Image, 123 00:05:23,795 --> 00:05:26,260 and then finally up to an object. 124 00:05:26,260 --> 00:05:28,620 Now that we're comfortable with the object, 125 00:05:28,620 --> 00:05:30,465 how do we view the image? 126 00:05:30,465 --> 00:05:33,740 It turns out that the image object has a show function. 127 00:05:33,740 --> 00:05:35,360 You can find this by looking at 128 00:05:35,360 --> 00:05:38,150 the properties of the object if you wanted to, 129 00:05:38,150 --> 00:05:39,785 using the dir function. 130 00:05:39,785 --> 00:05:42,030 So we'll call image.show, 131 00:05:42,030 --> 00:05:46,295 and that didn't seem to have the intended effect. 132 00:05:46,295 --> 00:05:47,930 The problem is the image is stored 133 00:05:47,930 --> 00:05:50,195 remotely on Coursera's server, 134 00:05:50,195 --> 00:05:52,900 but show tries to show it locally to you. 135 00:05:52,900 --> 00:05:55,670 So if the Coursera server software it was running 136 00:05:55,670 --> 00:05:58,625 on someone's workstation in Mountain View California, 137 00:05:58,625 --> 00:06:00,710 where Coursera has its offices, 138 00:06:00,710 --> 00:06:01,970 then you just popped up 139 00:06:01,970 --> 00:06:04,055 a picture of our recruitment materials. 140 00:06:04,055 --> 00:06:06,410 Thanks for that. Instead though, 141 00:06:06,410 --> 00:06:09,475 we want to render the image in the Jupyter notebook. 142 00:06:09,475 --> 00:06:10,940 It turns out Jupyter has 143 00:06:10,940 --> 00:06:13,175 a function which can help with this. 144 00:06:13,175 --> 00:06:15,440 So from IPython.display, 145 00:06:15,440 --> 00:06:17,280 we'll talk a little bit more about that, 146 00:06:17,280 --> 00:06:18,650 but IPython was one of 147 00:06:18,650 --> 00:06:22,250 the early terms for Jupyter and it started as 148 00:06:22,250 --> 00:06:25,625 just an interactive Python interpreter 149 00:06:25,625 --> 00:06:28,640 before moving into a much larger project. 150 00:06:28,640 --> 00:06:30,740 We want to import the display function, 151 00:06:30,740 --> 00:06:33,685 and then let's call display and pass it the image. 152 00:06:33,685 --> 00:06:36,950 Okay. So there we see our inline display of 153 00:06:36,950 --> 00:06:41,550 happy Masters of Science in Information students. 154 00:06:42,730 --> 00:06:45,110 For those who would like to understand 155 00:06:45,110 --> 00:06:46,235 this in more detail, 156 00:06:46,235 --> 00:06:49,010 the Jupyter environment is running a special wrapper 157 00:06:49,010 --> 00:06:52,240 across the Python interpreter called IPython. 158 00:06:52,240 --> 00:06:54,950 IPython allows the kernel back end to 159 00:06:54,950 --> 00:06:56,945 communicate with the browser front end 160 00:06:56,945 --> 00:06:58,595 among other things. 161 00:06:58,595 --> 00:07:01,550 The IPython package has a display function, 162 00:07:01,550 --> 00:07:03,230 which can take objects and use 163 00:07:03,230 --> 00:07:06,755 custom formatters in order to render them to the screen. 164 00:07:06,755 --> 00:07:10,250 There's a lot of different formatters provided including 165 00:07:10,250 --> 00:07:11,270 ones which know how to handle 166 00:07:11,270 --> 00:07:13,565 image types and different image types, 167 00:07:13,565 --> 00:07:15,785 and that's what we're using here. 168 00:07:15,785 --> 00:07:18,290 That's a quick overview of how to read 169 00:07:18,290 --> 00:07:20,525 and display images using pillow. 170 00:07:20,525 --> 00:07:22,310 In the next lecture, we're going to jump 171 00:07:22,310 --> 00:07:23,990 in a bit more into 172 00:07:23,990 --> 00:07:25,520 detail to understand how to 173 00:07:25,520 --> 00:07:28,500 use pillow to manipulate images.