Monday, November 24, 2025
HomeLanguagesPython PIL | MedianFilter() and ModeFilter() method

Python PIL | MedianFilter() and ModeFilter() method

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageFilter module contains definitions for a pre-defined set of filters,
which can be used with the Image.filter() method.

PIL.ImageFilter.MedianFilter() method creates a median filter. Picks the median pixel value in a window with the given size.

Syntax: PIL.ImageFilter.MedianFilter(size=3)

Parameters:
size: The kernel size, in pixels.

Image used:




# Importing Image and ImageFilter module from PIL package  
from PIL import Image, ImageFilter 
     
# creating a image object 
im1 = Image.open(r"C:\Users\sadow984\Desktop\download2.JPG") 
     
# applying the median filter 
im2 = im1.filter(ImageFilter.MedianFilter(size = 3)) 
     
im2.show() 


Output:

PIL.ImageFilter.ModeFilter() method creates a mode filter. 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.

Syntax: PIL.ImageFilter.ModeFilter(size=3)

Parameters:
size: The kernel size, in pixels.




# Importing Image and ImageFilter module from PIL package  
from PIL import Image, ImageFilter 
     
# creating a image object 
im1 = Image.open(r"C:\Users\sadow984\Desktop\download2.JPG") 
     
# applying the mode filter 
im2 = im1.filter(ImageFilter.ModeFilter(size = 3)) 
     
im2.show() 


Output:

RELATED ARTICLES

Most Popular

Dominic
32411 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6787 POSTS0 COMMENTS
Nicole Veronica
11934 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6910 POSTS0 COMMENTS
Ted Musemwa
7169 POSTS0 COMMENTS
Thapelo Manthata
6868 POSTS0 COMMENTS
Umr Jansen
6855 POSTS0 COMMENTS