ImageFilter Module

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

Example: Filter an image

  1. from PIL import ImageFilter
  2.  
  3. im1 = im.filter(ImageFilter.BLUR)
  4.  
  5. im2 = im.filter(ImageFilter.MinFilter(3))
  6. im3 = im.filter(ImageFilter.MinFilter) # same as MinFilter(3)

Filters

The current version of the library provides the following set of predefinedimage enhancement filters:

  • BLUR
  • CONTOUR
  • DETAIL
  • EDGE_ENHANCE
  • EDGE_ENHANCE_MORE
  • EMBOSS
  • FIND_EDGES
  • SHARPEN
  • SMOOTH
  • SMOOTH_MORE
  • class PIL.ImageFilter.Color3DLUT(size, table, channels=3, target_mode=None, **kwargs)[source]
  • Three-dimensional color lookup table.

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

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

New in version 5.2.0.

Parameters:

  • size – Size of the table. One int or tuple of (int, int, int).Minimal size in any dimension is 2, maximum is 65.
  • table – Flat lookup table. A list of channels size*3float elements or a list of size3 channels-sizedtuples with floats. Channels are changed first,then first dimension, then second, then third.Value 0.0 corresponds lowest value of output, 1.0 highest.
  • channels – Number of channels in the table. Could be 3 or 4.Default is 3.
  • target_mode – A mode for the result image. Should have not lessthan channels channels. Default is None,which means that mode wouldn’t be changed.
  • classmethod generate(size, callback, channels=3, target_mode=None)[source]
  • Generates new LUT using provided callback.

Parameters:

  1. - **size** Size of the table. Passed to the constructor.
  2. - **callback** Function with three parameters which correspondthree color channels. Will be called <code>size**3</code>times with values from 0.0 to 1.0 and should returna tuple with <code>channels</code> elements.
  3. - **channels** The number of channels which should return callback.
  4. - **target_mode** Passed to the constructor of the resultinglookup table.
  • transform(callback, with_normals=False, channels=None, target_mode=None)[source]
  • Transforms the table values using provided callback and returnsa new LUT with altered values.

Parameters:

  1. - **callback** A function which takes old lookup table valuesand returns a new set of values. The numberof arguments which function should take is<code>self.channels</code> or <code>3 + self.channels</code>if <code>with_normals</code> flag is set.Should return a tuple of <code>self.channels</code> or<code>channels</code> elements if it is set.
  2. - **with_normals** If true, <code>callback</code> will be called withcoordinates in the color cube as the firstthree arguments. Otherwise, <code>callback</code>will be called only with actual color values.
  3. - **channels** The number of channels in the resulting lookup table.
  4. - **target_mode** Passed to the constructor of the resultinglookup table.
  • class PIL.ImageFilter.BoxBlur(radius)[source]
  • Blurs the image by setting each pixel to the average value of the pixelsin a square box extending radius pixels in each direction.Supports float radius of arbitrary size. Uses an optimized implementationwhich runs in linear time relative to the size of the imagefor any radius value.

Parameters:radius – Size of the box in one direction. Radius 0 does not blur,returns an identical image. Radius 1 takes 1 pixelin each direction, i.e. 9 pixels in total.

  • class PIL.ImageFilter.GaussianBlur(radius=2)[source]
  • Gaussian blur filter.

Parameters:radius – Blur radius.

  • class PIL.ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3)[source]
  • Unsharp mask filter.

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

Parameters:

  • radius – Blur Radius
  • percent – Unsharp strength, in percent
  • threshold – Threshold controls the minimum brightness change thatwill be sharpened
  • class PIL.ImageFilter.Kernel(size, kernel, scale=None, offset=0)[source]
  • Create a convolution kernel. The current version onlysupports 3x3 and 5x5 integer and floating point kernels.

In the current version, kernels can only be applied to“L” and “RGB” images.

Parameters:

  • size – Kernel size, given as (width, height). In the currentversion, this must be (3,3) or (5,5).
  • kernel – A sequence containing kernel weights.
  • scale – Scale factor. If given, the result for each pixel isdivided by this value. the default is the sum of thekernel weights.
  • offset – Offset. If given, this value is added to the result,after it has been divided by the scale factor.
  • class PIL.ImageFilter.RankFilter(size, rank)[source]
  • Create a rank filter. The rank filter sorts all pixels ina window of the given size, and returns the rank’th value.

Parameters:

  • size – The kernel size, in pixels.
  • rank – What pixel value to pick. Use 0 for a min filter,size size / 2 for a median filter, size size - 1for a max filter, etc.
  • class PIL.ImageFilter.MedianFilter(size=3)[source]
  • Create a median filter. Picks the median pixel value in a window with thegiven size.

Parameters:size – The kernel size, in pixels.

  • class PIL.ImageFilter.MinFilter(size=3)[source]
  • Create a min filter. Picks the lowest pixel value in a window with thegiven size.

Parameters:size – The kernel size, in pixels.

  • class PIL.ImageFilter.MaxFilter(size=3)[source]
  • Create a max filter. Picks the largest pixel value in a window with thegiven size.

Parameters:size – The kernel size, in pixels.

  • class PIL.ImageFilter.ModeFilter(size=3)[source]
  • Create a mode filter. Picks the most frequent pixel value in a box with thegiven size. Pixel values that occur only once or twice are ignored; if nopixel value occurs more than twice, the original pixel value is preserved.

Parameters:size – The kernel size, in pixels.