Let's work out an example where a smoothing filter will be applied to an image.
Download the image file
0165_3_Before_SMOOTHING.png
from the Packt website and save it asBefore_SMOOTHING.png
.This is a low-resolution image scanned from a developed photograph. As you can see, there is a lot of salt-and-pepper noise in the image. We will apply smoothing filter to reduce some of this noise in the image data.
Add the following code in a Python file.
import ImageFilter import Image img = Image.open( "C:\\images\\Before_SMOOTH.png ") img = img.filter(ImageFilter.SMOOTH) img.save( "C:\\images\\ch3\\After_SMOOTH.png") img.show()
The highlighted line in the code is where the smoothing filter is applied to the image. The results are shown in the next illustration.
Picture before and after smoothing:
To reduce the noise further down, you can use
ImageFilter.SMOOTH_MORE
or try reapplying theImageFilter.SMOOTH
multiple times until you get the desired effect.import ImageFilter...