When your dataset consists of images, it is expedient to write programs that process them in batches. In this video, learn how to use the Python Imaging Library—Pillow—to load, resize, project, color, and blend images.
- [Instructor] The web is all about visually impact. But even scientific and engineering applications often require handling, analyzing, and modifying images encoding in many different formats. A powerful and reliable library to do so is a must. We could once rely on the beloved Python Imaging Library, developed by a Swedish software company. Unfortunately, its development was halted in 2009, which raises a big red flag, as we discussed in the introduction. Luckily, the library was resurrected as the Pillow Project, which is once again maintained and developed vigorously. We will experiment with Pillow on SDO images of the sun. We will experiment with Pillow on SDO images of the sun. This will give you only a slight taste of what's possible with the package. You can see the documentation for a lot more. So we start by important Pillow, as well as a few of its sub packages that we will need. We also need a couple more modules. In your exercise files, you will find two SDO images, taken on September 3 2013. And displaced by about eight hours. We load the first image from the corresponding file with PIL Image open. Jupiter displays the result in object for us. No questions asked. The object is in fact an instance of JPEG image file. If we had the image already within Python, for instance, say requests response object, we'd have to take a small detour and create a virtual file from this sequence of bytes. And here's the second picture of the sun. Let's see what we can do with these images. We can see their size and their mode. This case RGB corresponds to three channels with eight bits each. To resize an image, we can use the resize method of the Pillow object. Other useful methods include crop, rotate, and transform, which change the geometry of the image. Convert, which changes its representation. Filter, which applies transformations such as blurring, sharpening, and finding edges. For instance, if we wish to go black and white, we convert the image to luminance mode. Filter requires that we specify a filter from the Pillow collection. And these transformations can be changed. There's a reason why I gave you images separated by eight hours. Because the sun rotates a bit while its surface does not change very much, by waiting eight hours, it's as if we saw the sun from slightly different angles. So we can make one of those images that look three dimensionals when viewed with cardboard glasses with colored plastic lenses. Those images are known as an eclipse. And they are made by encoding each eyes image using a filter of different color. For instance, red and cyan. Let's try that with below. We first convert the resize images to gray scale, then use PIL image ops colorize, to map the range of gray scales to a range of colors. For the left image from black to red, which is 255 00 in RGB, and for the right image from black to cyan, which is 0 255 255. Then we blend them with equal weights using PIL image blend. Good, bit darker perhaps. To increase brightness, we use the Pillow image enhanced sub package, creative brightness object and then call it enhanced methods. In a similar way, we could also adjust contrast and color. So here we go. If you have red cyan glasses, see if the image looks three dimensional. If it looks wrong, turn the glasses around or swap the two images. Sometimes the colors are reversed. I think it's rather impressive that we were able to make an anaglyph with so little code. You may have noticed however, the Pillows interface was rather uneven. Resize and convert were methods of the image object. Colorize and blend were freestanding functions. To enhance brightness, we had to create a special brightness object and call it method. This is what happens when a package grows organically, with functionality introduced in modules developed by different people or at different times. And once the package is popular, it's too late to change the interface without breaking other programs that use it. And yet, Pillows power more than makes up for its flaws.