Applying Maximum Using Python And Pillow

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.

 

 

  • 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 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 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.


Copyright 2020 © pythontic.com