Python PIL’s MedianFilter() and ModeFilter() methods

Python PIL’s MedianFilter() and ModeFilter() Methods

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

PIL.ImageFilter.MedianFilter() creates a median filter. It selects the median pixel value within a window of a given size.

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

Parameters:
size: The kernel size, in pixels.

Images used:
Python PIL's MedianFilter() and ModeFilter() methods

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

Output:
MedianFilter() and ModeFilter() Methods in Python PIL

The PIL.ImageFilter.ModeFilter() method creates a mode filter. It selects the most frequent pixel value within a box of a 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 retained.

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

Parameters:
size: Kernel size, in pixels.

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

Output:
Python PIL's MedianFilter() and ModeFilter() methods

Leave a Reply

Your email address will not be published. Required fields are marked *