What are min and max filters in pillow in Python?
Overview
PIL or Python Imaging Library is an image processing library in Python. This library supports a wide range of file formats. It is designed for an efficient internal representation of image data and provides powerful image processing capabilities.
The ImageFilter Module
The ImageFilter module from PIL contains definitions for a predefined set of filters that are used for different purposes.
The MinFilter function
The MinFilter function creates a min filter. This is used to pick the lowest pixel value in a window of a given size.
Syntax
PIL.ImageFilter.MinFilter(size=3)
Parameter
size: This represents the window size.
The MaxFilter function
The MaxFilter function creates a min filter. This is used to pick the largest pixel value in a window of a given size.
Syntax
PIL.ImageFilter.MaxFilter(size=3)
Parameter
size: This represents the window size.
Example
from PIL import Image, ImageFilterimport matplotlib.pyplot as pltimg = Image.open("man.png")filter_size = 3min_img = img.filter(ImageFilter.MinFilter(size=filter_size))min_img.save("min_img.png")max_img = img.filter(ImageFilter.MaxFilter(size=filter_size))max_img.save("max_img.png")
Explanation
- Line 1: We import the
ImageFilterandImagemodules. - Line 3: We use
open()from theImagemodule to readman.pnginto memory. - Line 5: We define the filter size,
filter_size. - Line 7: We apply the
MinFilterwith size asfilter_sizeto the image we loaded in line 3. - Line 9: We save the output of
MinFilterto disk. - Line 11: We apply the
MaxFilterwith size asfilter_sizeto the image we loaded in line 3. - Line 13: We save the output of
MaxFilterto disk.