ImageFilter Module (original) (raw)

View this page

Toggle table of contents sidebar

The ImageFilter module contains definitions for a pre-defined set of filters, which can be be used with the Image.filter() method.

Example: Filter an image

from PIL import ImageFilter

im1 = im.filter(ImageFilter.BLUR)

im2 = im.filter(ImageFilter.MinFilter(3)) im3 = im.filter(ImageFilter.MinFilter) # same as MinFilter(3)

Filters

Pillow provides the following set of predefined image enhancement filters:

class PIL.ImageFilter.Color3DLUT(size: int | tuple[int, int, int], table: Sequence[float] | Sequence[Sequence[int]] | NumpyArray, channels: int = 3, target_mode: str | None = None, **kwargs: bool)[source]

Three-dimensional color lookup table.

Transforms 3-channel pixels using the values of the channels as coordinates in the 3D lookup table and interpolating the nearest elements.

This method allows you to apply almost any color transformation in constant time by using pre-calculated decimated tables.

Added in version 5.2.0.

Parameters:

classmethod generate(size: int | tuple[int, int, int], callback: Callable[[float, float, float], tuple[float, ...]], channels: int = 3, target_mode: str | None = None) → Color3DLUT[source]

Generates new LUT using provided callback.

Parameters:

transform(callback: Callable[[...], tuple[float, ...]], with_normals: bool = False, channels: int | None = None, target_mode: str | None = None) → Color3DLUT[source]

Transforms the table values using provided callback and returns a new LUT with altered values.

Parameters:

class PIL.ImageFilter.BoxBlur(radius: float | Sequence[float])[source]

Blurs the image by setting each pixel to the average value of the pixels in a square box extending radius pixels in each direction. Supports float radius of arbitrary size. Uses an optimized implementation which runs in linear time relative to the size of the image for any radius value.

Parameters:

radius

Size of the box in a direction. Either a sequence of two numbers for x and y, or a single number for both.

Radius 0 does not blur, returns an identical image. Radius 1 takes 1 pixel in each direction, i.e. 9 pixels in total.

class PIL.ImageFilter.GaussianBlur(radius: float | Sequence[float] = 2)[source]

Blurs the image with a sequence of extended box filters, which approximates a Gaussian kernel. For details on accuracy see <https://www.mia.uni-saarland.de/Publications/gwosdek-ssvm11.pdf>

Parameters:

radius – Standard deviation of the Gaussian kernel. Either a sequence of two numbers for x and y, or a single number for both.

class PIL.ImageFilter.UnsharpMask(radius: float = 2, percent: int = 150, threshold: int = 3)[source]

Unsharp mask filter.

See Wikipedia’s entry on digital unsharp masking for an explanation of the parameters.

Parameters:

class PIL.ImageFilter.Kernel(size: tuple[int, int], kernel: Sequence[float], scale: float | None = None, offset: float = 0)[source]

Create a convolution kernel. This only supports 3x3 and 5x5 integer and floating point kernels.

Kernels can only be applied to “L” and “RGB” images.

Parameters:

class PIL.ImageFilter.RankFilter(size: int, rank: int)[source]

Create a rank filter. The rank filter sorts all pixels in a window of the given size, and returns the rank’th value.

Parameters:

class PIL.ImageFilter.MedianFilter(size: int = 3)[source]

Create a median filter. Picks the median pixel value in a window with the given size.

Parameters:

size – The kernel size, in pixels.

class PIL.ImageFilter.MinFilter(size: int = 3)[source]

Create a min filter. Picks the lowest pixel value in a window with the given size.

Parameters:

size – The kernel size, in pixels.

class PIL.ImageFilter.MaxFilter(size: int = 3)[source]

Create a max filter. Picks the largest pixel value in a window with the given size.

Parameters:

size – The kernel size, in pixels.

class PIL.ImageFilter.ModeFilter(size: int = 3)[source]

Create a mode filter. Picks the most frequent pixel value in a box with the given size. Pixel values that occur only once or twice are ignored; if no pixel value occurs more than twice, the original pixel value is preserved.

Parameters:

size – The kernel size, in pixels.

class PIL.ImageFilter.Filter[source]

An abstract mixin used for filtering images (for use with filter()).

Implementors must provide the following method:

filter(self, image)[source]

Applies a filter to a single-band image, or a single band of an image.

Returns:

A filtered copy of the image.

class PIL.ImageFilter.MultibandFilter[source]

An abstract mixin used for filtering multi-band images (for use with filter()).

Implementors must provide the following method:

filter(self, image)

Applies a filter to a multi-band image.

Returns:

A filtered copy of the image.