Smoothing an image is helpful for a few different reasons. The big one that I’ve been using image smoothing for is to remove noise from an image. Laplacian edge detection is highly susceptible to noise due to the Laplacian operator being a second derivative operator. The Python Imaging Library (PIL) provides a few different ways to produce smooth images. The most obvious would be to use the ImageFilter.SMOOTH class.

The image I’ll be working on throughout this post is this greyscale picture of me:

Standard smoothing in PIL is super easy. For example:

Using the image of me above, here’s the result of this smoothing operation:

That’s a pretty good result for just using stock PIL image filters. The Python Imaging Library also offers a SMOOTH_MORE filter. Replacing the ImageFilter.SMOOTH above with ImageFilter.SMOOTH_MORE, we get:

Digging into the PIL source gives a really good indication of how it produces these results. For example, the BuiltinFilter classes (such as ImageFilter.SMOOTH) use filter arguments to produce different results. These filter arguments are a size tuple, which is the width and height of the kernel, the convolution kernel itself as a sequence containing weighted values, the scale which is used to divide the result of each pixel, and finally the offset which is added to the result after it has been divided by the scale factor.

For ImageFilter.SMOOTH, these filter arguments are:

I’m going to present a way to implement image smoothing so you can have a better idea of what’s really going on when you smooth an image. One small note: the scale part of the process defaults to the sum of the weights in the kernel. So if it isn’t present, you can calculate the default scale by doing this:

With that out of the way, here’s an implementation of image smoothing that is equivalent to calling ImageFilter.SMOOTH like the code above:

Notice how it loops through 1 <= y < height-1 and 1 <= x < width? That's because doing this processing through the entire image produces weird borders. The code above compensates for this and eliminates the dark borders by copying the original border pixels to the borders of the output image.

So what happens when I run the code above on the original image? Check it out:

Being able to code this kind of stuff is really cool, but for these really basic examples of standard smoothing, using PIL’s built-in filters is definitely the way to go. You’re also not limited to using PIL’s built-in filters. If you require different kernels and even different scaling and offset attributes, PIL provides ways for you to do that.

I was hoping to cover the ways you can implement your own filters in PIL, but it’s getting late so I will try covering them tomorrow if I have time.

Leave a Reply