Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Blurring, Sharpening, and Smoothing

00:00 Image Blurring, Sharpening, and Smoothing. You’ll return to using the image of the buildings that you used at the beginning of the course. You can start a new REPL session for this section.

00:11 In addition to Image, you also import the ImageFilter module from Pillow. You then load the building image with a code you’ve seen before.

00:30 You can use the .filter() method to apply filtering to the image. This method needs a convolution kernel as its argument, and you can use one of the several kernels available in the ImageFilter module in Pillow. The first set of filters that you’ll learn about deal with blurring, sharpening, and smoothing an image.

00:49 You can blur the image using the predefined ImageFilter.BLUR filter.

01:03 The displayed image is a blurred version of the original one. You can zoom in to observe the difference in more detail using .crop() and then display the images once more using .show().

01:17 You show a cropped version of the original, followed by a crop of the blurred image.

01:30 The two cropped images are shown side by side on screen. You can customize the type and amount of blurring that you need using ImageFilter.BoxBlur()

01:58 or ImageFilter.GaussianBlur(). For an easier comparison, you can see the three blurred images on-screen with order of creation in left to right.

02:15 The .BoxBlur() filter is similar to the one described in a previous section introducing convolution kernels. The argument is the radius of the box blur filter. In the kernel section, the box blur filter you used was a 3x3 filter.

02:30 This means it had a radius of one because the filter extends by one pixel from the center. These images show that the box blur filter with a radius of 20 produces a more blurred image than a box blur with a radius of five.

02:45 You can also use the .GaussianBlur() filter, which uses a Gaussian blur kernel. The Gaussian kernel puts more weight on the pixels at the center of the kernel than those at the edges, and this leads to a smoother blurring than the box blur.

02:59 For this reason, Gaussian blurring can give better results in many cases. But what if you want to sharpen an image? In that case, you can use the ImageFilter.SHARPEN filter and compare the result with the original image.

03:22 The cropped area of the original image and the sharpened image are shown to make the comparison easier. They’re shown on-screen next to each other, with the sharpened image on the right. Perhaps instead of sharpening an image, you need to smooth it.

03:47 You can achieve this by passing ImageFilter.SMOOTH as an argument to .filter().

04:01 Again, the cropped area of the original and smooth version are shown on-screen. You can see the original image on the left and the smooth image on the right.

04:20 In the next section of the course, you’ll learn about filters in the ImageFilter module which act on the edges of objects in the image, as well as an application of the smooth filter.

Become a Member to join the conversation.