Python PIL | MedianFilter () and ModeFilter () method

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

  Syntax:  PIL.ImageFilter.MedianFilter (size = 3)  Parameters:   size:  The kernel size, in pixels. 

Image used:

# Import Image and ImageFilter module from PIL

from PIL import Image, ImageFilter 

 
# create image object

im1 = Image. open (r " C: Userssadow984Desktopdownload2.JPG "

  
# applying median filter

im2 = im1. filter (ImageFilter.MedianFilter (size = 3 )) 

 
im2.show () 

Output:

PIL.ImageFilter.ModeFilter() creates a mode filter. Selects the most common pixel value in the specified field size. Pixel values ​​that occur only once or twice are ignored; if the pixel value does not occur more than twice, the original pixel value is retained.

  Syntax:  PIL.ImageFilter.ModeFilter (size = 3)  Parameters:   size:  The kernel size, in pixels. 

# Import Image and ImageFilter module from PIL

from PIL import Image, ImageFilter 

 
# create an image object

im1 = Image. open (r "C: Userssadow984Desktopdownload2.JPG"

  
# applying a mode filter

im2 = im1. filter (ImageFilter. ModeFilter (size = 3 )) 

 
im2.show () 

Output:





Get Solution for free from DataCamp guru