Skip to content

ImageFilter

from blanket import ImageFilter provides Pillow-compatible built-in convolution filters, custom Kernel filters, RankFilter, MedianFilter, MinFilter, MaxFilter, ModeFilter, BoxBlur, GaussianBlur, UnsharpMask, and Color3DLUT (including generate and transform). Filtering supports Blanket's L, RGB, and RGBA images; color LUTs require RGB or RGBA input. Pixel kernels run in Rust and release the GIL, with parallel output partitions for large images. Python handles filter configuration and user callbacks for generating LUT tables.

from blanket import ImageFilter

blurred = image.filter(ImageFilter.GaussianBlur(radius=2))
sharpened = image.filter(ImageFilter.SHARPEN)

Image.filter accepts filter instances, classes, and custom Filter or MultibandFilter subclasses. Convolution preserves the image border; rank filters extend edge pixels, while mode filters use the available neighborhood. Blurs accept a scalar radius or separate (x, y) radii. Non-finite or excessively large blur radii are rejected.

Reference

Kernel

Kernel(size: tuple[int, int], kernel: Sequence[float], scale: float | None = None, offset: float = 0)

Bases: BuiltinFilter

A 3x3 or 5x5 convolution, with vertically flipped kernel rows.

Configure Kernel.

Parameters:

Name Type Description Default
size tuple[int, int]

Kernel dimensions: (3, 3) or (5, 5).

required
kernel Sequence[float]

Row-major convolution coefficients for a 3 by 3 or 5 by 5 kernel.

required
scale float | None

Divisor for the kernel coefficients; None uses their sum, or 1 when the sum is zero.

None
offset float

Value added after scaling.

0

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.Kernel((3, 3), [1] * 9, scale=9))

RankFilter

RankFilter(size: int, rank: int)

Bases: Filter

Select the zero-based rank in an odd-sized, edge-extended window.

Configure RankFilter.

Parameters:

Name Type Description Default
size int

Odd neighborhood width in pixels.

required
rank int

Zero-based rank within the window, from 0 through size squared minus 1.

required

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.RankFilter(size=3, rank=4))

MedianFilter

MedianFilter(size: int = 3)

Bases: RankFilter

Configure MedianFilter.

Parameters:

Name Type Description Default
size int

Odd neighborhood width in pixels.

3

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.MedianFilter(size=3))

MinFilter

MinFilter(size: int = 3)

Bases: RankFilter

Configure MinFilter.

Parameters:

Name Type Description Default
size int

Odd neighborhood width in pixels.

3

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.MinFilter(size=3))

MaxFilter

MaxFilter(size: int = 3)

Bases: RankFilter

Configure MaxFilter.

Parameters:

Name Type Description Default
size int

Odd neighborhood width in pixels.

3

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.MaxFilter(size=3))

ModeFilter

ModeFilter(size: int = 3)

Bases: Filter

Use the most frequent nearby value when it appears at least three times.

Configure ModeFilter.

Parameters:

Name Type Description Default
size int

Odd neighborhood width in pixels.

3

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.ModeFilter(size=3))

GaussianBlur

GaussianBlur(radius: float | Sequence[float] = 2)

Bases: MultibandFilter

Approximate Gaussian blur with three extended box passes per axis.

Configure GaussianBlur.

Parameters:

Name Type Description Default
radius float | Sequence[float]

Nonnegative blur radius, or separate horizontal and vertical radii.

2

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.GaussianBlur(radius=2))

BoxBlur

BoxBlur(radius: float | Sequence[float])

Bases: MultibandFilter

Blur with a fractional box radius, in linear time per pass.

Configure BoxBlur.

Parameters:

Name Type Description Default
radius float | Sequence[float]

Nonnegative blur radius, or separate horizontal and vertical radii.

required

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.BoxBlur(radius=2))

UnsharpMask

UnsharpMask(radius: float = 2, percent: int = 150, threshold: int = 3)

Bases: MultibandFilter

Configure UnsharpMask.

Parameters:

Name Type Description Default
radius float

Nonnegative Gaussian blur radius.

2
percent int

Sharpening strength as a percentage.

150
threshold int

Intensity threshold, on the 0 through 255 scale.

3

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3))

Color3DLUT

Color3DLUT(size: int | tuple[int, int, int], table: Sequence[float] | Sequence[Sequence[float]] | Any, channels: int = 3, target_mode: str | None = None, **kwargs: bool)

Bases: MultibandFilter

A mutable three-dimensional color table with native trilinear interpolation.

Configure Color3DLUT.

Parameters:

Name Type Description Default
size int | tuple[int, int, int]

Grid size from 2 through 65, or separate sizes for the red, green, and blue axes.

required
table Sequence[float] | Sequence[Sequence[float]] | Any

Flat or nested RGB/RGBA values for every table point, with red varying fastest; values use the 0 through 1 scale.

required
channels int

Number of output channels, 3 or 4.

3
target_mode str | None

Optional output mode, such as RGB or RGBA.

None
**kwargs bool

Internal table-copy option; leave omitted for normal use.

{}

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
table = [(r, g, b) for b in (0, 1) for g in (0, 1) for r in (0, 1)]
lut = ImageFilter.Color3DLUT(2, table)
result = image.filter(lut)

generate classmethod

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

Create a color lookup table by evaluating a callback on a regular grid.

Parameters:

Name Type Description Default
size int | tuple[int, int, int]

Grid size from 2 through 65, or separate sizes for the red, green, and blue axes.

required
callback Callable[[float, float, float], tuple[float, ...]]

Callable receiving normalized red, green, blue coordinates and returning one output tuple.

required
channels int

Number of output channels, 3 or 4.

3
target_mode str | None

Optional output mode, such as RGB or RGBA.

None

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
lut = ImageFilter.Color3DLUT.generate(3, lambda r, g, b: (1 - r, g, b))
result = image.filter(lut)

transform

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

Return a new lookup table by transforming the current table entries.

Parameters:

Name Type Description Default
callback Callable[..., tuple[float, ...]]

Callable receiving current table channel values and returning new output values.

required
with_normals bool

Prepend normalized red, green, blue coordinates to the callback's input channel values.

False
channels int | None

Output channel count, or None to retain the existing count.

None
target_mode str | None

Optional output mode, such as RGB or RGBA.

None

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
lut = ImageFilter.Color3DLUT.generate(3, lambda r, g, b: (r, g, b))
adjusted = lut.transform(lambda r, g, b: (r * 0.8, g, b))
result = image.filter(adjusted)

BLUR

Bases: BuiltinFilter

Blur with a fixed 5 by 5 convolution.

This filter takes no parameters. Pass the class or an instance to Image.filter.

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.BLUR)

CONTOUR

Bases: BuiltinFilter

Emphasize outlines with a 3 by 3 contour kernel.

This filter takes no parameters. Pass the class or an instance to Image.filter.

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.CONTOUR)

DETAIL

Bases: BuiltinFilter

Enhance fine detail with a 3 by 3 convolution.

This filter takes no parameters. Pass the class or an instance to Image.filter.

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.DETAIL)

EDGE_ENHANCE

Bases: BuiltinFilter

Enhance edges with a 3 by 3 convolution.

This filter takes no parameters. Pass the class or an instance to Image.filter.

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.EDGE_ENHANCE)

EDGE_ENHANCE_MORE

Bases: BuiltinFilter

Apply a stronger 3 by 3 edge enhancement.

This filter takes no parameters. Pass the class or an instance to Image.filter.

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.EDGE_ENHANCE_MORE)

EMBOSS

Bases: BuiltinFilter

Create an embossed appearance.

This filter takes no parameters. Pass the class or an instance to Image.filter.

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.EMBOSS)

FIND_EDGES

Bases: BuiltinFilter

Extract edges with a 3 by 3 convolution.

This filter takes no parameters. Pass the class or an instance to Image.filter.

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.FIND_EDGES)

SHARPEN

Bases: BuiltinFilter

Sharpen with a fixed 3 by 3 convolution.

This filter takes no parameters. Pass the class or an instance to Image.filter.

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.SHARPEN)

SMOOTH

Bases: BuiltinFilter

Smooth with a fixed 3 by 3 convolution.

This filter takes no parameters. Pass the class or an instance to Image.filter.

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.SMOOTH)

SMOOTH_MORE

Bases: BuiltinFilter

Apply stronger smoothing with a 5 by 5 convolution.

This filter takes no parameters. Pass the class or an instance to Image.filter.

Examples:

from blanket import Image, ImageFilter

image = Image.new("RGB", (8, 8), (40, 100, 180))
result = image.filter(ImageFilter.SMOOTH_MORE)