Applying Minimum Filter Using Python And Pillow

Overview:

  • When the minimum filter is applied to a digital image it picks up the minimum value of the neighbourhood pixel window and assigns it to the current pixel. A pixel with the minimum value is the darkest among the pixels present in the pixel window. 
  • The dark values present in an image are enhanced by the minimum filter.
  • Minimum filter is also called as a dilation filter. When minimum filter is applied the object boundaries present in an image are extended. 
  • The minimum filter is one of the morphological filters. The other morphological filters include maximum filter and the median filter.
  • The minimum filter removes any positive outlier noise present in a digital image.

 

Example:

# -----Python example program for applying minimum filter to a Digital Image-----

 

# import the required PIL Modules

from PIL import Image

from PIL import ImageFont

from PIL import ImageDraw

from PIL import ImageFilter

 

# Draw text on the image

def writeText(baseImage, imageDescription, textSize, textX, textY, fontFileLocation):

    baseImage = baseImage.convert('RGBA');   

    textImage = Image.new('RGBA', baseImage.size, (255,255,255,0));

 

    # Select a font for the text

    font = ImageFont.truetype(fontFileLocation, textSize);

    draw = ImageDraw.Draw(textImage);

    draw.text((textX,textY), imageDescription, font=font, fill=(255,255,255,255));

 

    # Do an alpha composite of the two images and return

    return Image.alpha_composite(baseImage, textImage);   

    

# Text size and location

textSize = 150;

textX    = 20;

textY    = 60;

 

fontFileLocation    = "/opt/X11/share/fonts/TTF/luxirr.ttf";

imageFilePath       = "./droplets.jpg";

imageText           = "Before applying minimum filter:";

 

# Create an image object from a file

imageInstance = Image.open(imageFilePath);

originalImage = writeText(imageInstance, imageText, textSize, textX, textY, fontFileLocation);

originalImage.show();

 

# Apply minimum filter twice to the image

minFilter1x   = imageInstance.filter(ImageFilter.MinFilter);

imageText     = "Minimum filter-1x:";

min1xWithText = writeText(minFilter1x, imageText, textSize, textX, textY, fontFileLocation);       

 

minFilter2x   = minFilter1x.filter(ImageFilter.MinFilter);

imageText     = "Minimum filter-2x:";

min2xWithText = writeText(minFilter2x, imageText, textSize, textX, textY, fontFileLocation);       

 

min1xWithText.show();

min2xWithText.show();

 

Output:

Before applying minimum filter:

Before applying minimum filter using Pillow - The Python Image Processing Library

After applying minimum filter 1x times:

After applying minimum filter 1x times

After applying minimum filter 2x times:

After applying minimum filter 2x times


Copyright 2023 © pythontic.com