Image Processing
00:00 Image Processing Using Pillow in Python. Previously in the course, you’ve learned how to crop and rotate images, resize them, and extract color bands from color images. However, none of the actions that you’ve taken so far have made any changes to the content of the image. In these sections of the course, you’ll learn about image processing features in the Python Pillow library.
00:23
To do this, you’ll make use of the ImageFilter
module in Pillow.
00:29 One of the methods that’s used in image processing is image convolution using kernels. A detailed explanation of image processing theory is outside the scope of this course, but if you’re interested in the science of image processing, one of the best resources you can use is Digital Image Processing by Gonzales and Woods, which is seen on-screen. In this section, you’ll learn the basics of how you can use convolution kernels to perform image processing. But what is a convolution kernel?
00:58 A kernel is a matrix. You can consider a simple image to understand the process of convolution using kernels. The image has a size of 30x30 pixels and contains a vertical line and a dot.
01:12 The line is four pixels wide, and the dot consists of a 4x4 pixel square. The image is seen on-screen, enlarged for display purposes. You can place the kernel anywhere on the image and use the location of the kernel’s central cell as a reference.
01:28 The diagram on-screen is a representation of the top-left portion of the image. The white squares represent pixels in the image that have a value of zero.
01:37 The red squares represent pixels in the image that have a value of 255. Each purple region represents the kernel. The diagram shows a kernel in three different positions labeled 1, 2, and 3.
01:51 A new image can be created as a result of the convolution of the image with the kernel. Let’s take a look at kernel position 1. The position of the kernel is (3, 2), which is the position of its central cell because it’s in the fourth row with index of 3, and the third column with index of 2.
02:09 Each image pixel in the region covered by the kernel has a value of zero, so therefore, all of the multiplication from step 2 will be zero, and the addition will also be zero.
02:21 The new image will have a value of zero at pixel (3, 2).
02:26 In the case of the other kernel positions, the situation is different. Take a look at kernel position 2, located at (4, 7). One of the image pixels overlapping this is not zero.
02:37 The multiplication of this pixel value, 255, multiplied by the kernel value of 1/9, gives 28.33. The eight remaining multiplications are still zero because all of those other image pixels are zero.
02:53 This will mean the value at pixel position (4, 7) would be 28.33.
03:00 The third kernel position is at (8, 11). There are four non-zero image pixels overlapping with this kernel. Each one has a value of 255, so the multiplication result for each of them will be 28.33.
03:15 The overall result for this kernel position is 113.33, and the new image will have this value at (8, 11).
03:24 Remember that this is only the three kernel positions shown in the diagram. The convolution process repeats this process for every possible kernel position in the image, which gives a new value for each pixel position. In the image
03:38 on-screen, you can see the result of the convolution shown on the right, with the original image on the left.
03:45 The kernel you saw previously is called a box blur kernel. The factor of 1/9 is there so that the overall weighting of the kernel is one, as it covers nine pixels.
03:55 The result of the convolution is a blurred version of the original image.
04:01 There are other kernels that perform different functions, including different blurring methods, edge detection, sharpening, and more.
04:11
The Pillow library has several built-in kernels and functions that will perform the convolution described here. You don’t need to understand the math of filtering through convolution to use these filters, but it always helps to know what’s happening behind the scenes when using these tools. The following sections of the course will look at the kernels and image filtering capabilities available in the ImageFilter
module in Pillow, starting by looking at blurring, sharpening, and smoothing.
Become a Member to join the conversation.