14. ImageFilter ModeFilter
14.1. ModeFilter
Use the
Image.filter(ImageFilter.ModeFilter(radius=3))
method to use the mode of the pixel value in a pixel area given by the radius.Picks the most frequent pixel value in a box with the given size. Pixel values that occur only once or twice are ignored; if no pixel value occurs more than twice, the original pixel value is preserved.
from PIL import Image, ImageFilter
with Image.open("test_images/river_valley.jpg") as im:
new_im = im.filter(ImageFilter.ModeFilter(size=3))
new_im.save("filters/ModeFilter.png")
Unfiltered, MinFilter, MedianFilter, ModeFilter and MaxFilter are compared below: