Applying Maximum filter using Python and Pillow

The maximum filter replaces the original pixel value with the maximum value of it neighbourhood pixels. Since the maximum values correspond to brighter pixels the max filter removes darker regions from a Digital Image. The example python program continuously applies the max filter ten times on to the original image and displays the output.

Pillow Updated 12 Jun 2019 2 min read

Overview

  • The maximum filter replaces each pixel value of a Digital Image with the maximum value(i.e., the value of the brightest pixel) of its neighbourhood pixel window. It is the opposite of what the minimum filter does to an Image.

  • Applying the maximum filter removes the negative outlier noise present in a Digital Image.

  • When a maximum filter is applied, the darker objects present in the image are eroded. This way maximum filter is called an erosion filter. With respect to the lighter pixels, some call this as a dilation filter.

  • To be clear, brighter objects are dilated and the darker objects are eroded upon applying a maximum filter to a Digital Image.

Applying max filter using Pillow

  • To apply any filter to an Image using Python and Pillow, the first step is to load the image from a file using Image.open(). This creates an Image object and loads the image information into it.

  • The ImageModule of Pillow - the Python Image Processing Library provides various filters that can be applied onto a Digital Image. The collection of filters include maximum filter, minimum filter, median filter, blur filter, sharpening filter, smoothening filter and other filters.

  • Invoking the filter() method with the name of a filter class applies the required filter to an Image. In the example provided, filter() method is called with ImageFilter.MaxFilter.

  • The example Python program applies ImageFilter.MaxFilter ten times in a for loop. In the resultant image, the black regions are eroded with brighter pixels.

Example

python
#----- Python example program for applying a maximum filter to a digital image -----

            from PIL import Image

            from PIL import ImageFilter

# Method to apply the filter

            def applyMaximumFilter(image):

                return image.filter(ImageFilter.MaxFilter);

# Load the image

            imagePath   = "./raindrops.jpg";

            imageObject = Image.open(imagePath);

# Apply maximum filter

            filterApplied = imageObject;

            for i in range(0, 10):

                print(i);

                filterApplied = applyMaximumFilter(filterApplied);

# Display images

            imageObject.show();

            filterApplied.show();

Output

Before applying the maximum filter to the Digital Image using Python and Pillow

Before applying the maximum filter using pillow - the python image processing library.

After applying the maximum filter 10x times to the Digital Image using Python and Pillow

After applying the maximum filter 10x times using pillow - the python image processing library.