Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What are min and max filters in pillow in Python?

Ravi

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, ImageFilter
import matplotlib.pyplot as plt

img = Image.open("man.png")

filter_size = 3

min_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 ImageFilter and Image modules.
  • Line 3: We use open() from the Image module to read man.png into memory.
  • Line 5: We define the filter size, filter_size.
  • Line 7: We apply the MinFilter with size as filter_size to the image we loaded in line 3.
  • Line 9: We save the output of MinFilter to disk.
  • Line 11: We apply the MaxFilter with size as filter_size to the image we loaded in line 3.
  • Line 13: We save the output of MaxFilter to disk.

RELATED TAGS

python
RELATED COURSES

View all Courses

Keep Exploring