ImageFilter
Module¶
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¶
The current version of the library provides the following set of predefined image 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)[源代码]¶ 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.
5.2.0 新版功能.
- 参数
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**3
float elements or a list ofsize**3
channels-sized tuples 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 less than
channels
channels. Default isNone
, which means that mode wouldn’t be changed.
-
classmethod
generate
(size, callback, channels=3, target_mode=None)[源代码]¶ Generates new LUT using provided callback.
- 参数
size – Size of the table. Passed to the constructor.
callback – Function with three parameters which correspond three color channels. Will be called
size**3
times with values from 0.0 to 1.0 and should return a tuple withchannels
elements.channels – The number of channels which should return callback.
target_mode – Passed to the constructor of the resulting lookup table.
-
transform
(callback, with_normals=False, channels=None, target_mode=None)[源代码]¶ Transforms the table values using provided callback and returns a new LUT with altered values.
- 参数
callback – A function which takes old lookup table values and returns a new set of values. The number of arguments which function should take is
self.channels
or3 + self.channels
ifwith_normals
flag is set. Should return a tuple ofself.channels
orchannels
elements if it is set.with_normals – If true,
callback
will be called with coordinates in the color cube as the first three arguments. Otherwise,callback
will be called only with actual color values.channels – The number of channels in the resulting lookup table.
target_mode – Passed to the constructor of the resulting lookup table.
-
class
PIL.ImageFilter.
BoxBlur
(radius)[源代码]¶ 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.
- 参数
radius – Size of the box in one direction. 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.
UnsharpMask
(radius=2, percent=150, threshold=3)[源代码]¶ Unsharp mask filter.
See Wikipedia’s entry on digital unsharp masking for an explanation of the parameters.
- 参数
radius – Blur Radius
percent – Unsharp strength, in percent
threshold – Threshold controls the minimum brightness change that will be sharpened
-
class
PIL.ImageFilter.
Kernel
(size, kernel, scale=None, offset=0)[源代码]¶ Create a convolution kernel. The current version only supports 3x3 and 5x5 integer and floating point kernels.
In the current version, kernels can only be applied to “L” and “RGB” images.
- 参数
size – Kernel size, given as (width, height). In the current version, this must be (3,3) or (5,5).
kernel – A sequence containing kernel weights.
scale – Scale factor. If given, the result for each pixel is divided by this value. the default is the sum of the kernel 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)[源代码]¶ Create a rank filter. The rank filter sorts all pixels in a window of the given size, and returns the rank’th value.
- 参数
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 - 1
for a max filter, etc.
-
class
PIL.ImageFilter.
MedianFilter
(size=3)[源代码]¶ Create a median filter. Picks the median pixel value in a window with the given size.
- 参数
size – The kernel size, in pixels.
-
class
PIL.ImageFilter.
MinFilter
(size=3)[源代码]¶ Create a min filter. Picks the lowest pixel value in a window with the given size.
- 参数
size – The kernel size, in pixels.
-
class
PIL.ImageFilter.
MaxFilter
(size=3)[源代码]¶ Create a max filter. Picks the largest pixel value in a window with the given size.
- 参数
size – The kernel size, in pixels.
-
class
PIL.ImageFilter.
ModeFilter
(size=3)[源代码]¶ 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.
- 参数
size – The kernel size, in pixels.