Filters
A filter looks at the neighbourhood of every pixel and writes a new value. Each one returns a new image and leaves its input alone and works on 1 to 4 channels. Every channel is filtered, alpha included, except where a filter says otherwise: bilateral_filter copies the alpha, unsharp_mask never changes it, and pixelize weights the color by it. The SIMD code and the threads (a big picture is split between them) give the same bytes as the plain code on every machine.
import os
import libstb as stb
os.makedirs("out", exist_ok=True)
img = stb.Image.open("astronaut.png").thumbnail(256, 256)
# Each filter returns a new image and leaves its input alone.
results = {
"gaussian": stb.gaussian_blur(img, 3.0), # sigma, in pixels
"box": stb.box_blur(img, 4), # radius
"median": stb.median_filter(img, 2), # radius
"bilateral": stb.bilateral_filter(img, 5, 3.0, 0.1), # radius, sigma_space, sigma_range (0 to 1)
"unsharp": stb.unsharp_mask(img, 2.0, 1.5), # sigma, amount
"pixelize": stb.pixelize(img, 12), # block size
}
for name, result in results.items():
result.write(f"out/{name}.png")
print(f"{name}: mean absolute change {stb.mae(img, result):.2f}")
# Canny finds edges: a gray mask, 255 on an edge, one pixel wide.
edges = stb.canny(img.convert(stb.PixelFormat.GRAY), 0.1, 0.2, 1.0)
edges.write("out/canny.png")
# The edge mode says what lies outside the picture when a filter reaches past the border.
zero = stb.gaussian_blur(img, 10.0, edge="zero")
mirror = stb.gaussian_blur(img, 10.0, edge="mirror")
print(f"zero against mirror at the border: {stb.mae(zero, mirror):.2f}")
gaussian: mean absolute change 15.34
box: mean absolute change 15.14
median: mean absolute change 5.30
bilateral: mean absolute change 2.73
unsharp: mean absolute change 12.42
pixelize: mean absolute change 25.56
zero against mirror at the border: 6.48







What each one does
- gaussian_blur(sigma) blurs with a Gaussian of the given standard deviation in pixels. It is the one to reach for first.
- box_blur(radius) averages the square of side
2 * radius + 1. It is the fastest blur and a little blocky. - median_filter(radius) takes the median of the same square, per channel. It removes isolated noise (salt and pepper) and keeps edges.
- bilateral_filter(radius, sigma_space, sigma_range) smooths without blurring edges: a neighbour counts less the farther it is (
sigma_space, in pixels) and the more its color differs (sigma_range, as a fraction of the full range, 0 to 1). - unsharp_mask(sigma, amount, threshold) sharpens by adding back
amounttimes the difference from a blur. An amount of 1 is "100 %" of Pillow's UnsharpMask. A difference belowthresholdis left alone. - pixelize(block_w, block_h) replaces every block by its mean.
- canny(low, high, sigma) returns the edges as a gray mask. It blurs by
sigma, takes the Sobel gradient, thins it to a ridge and keeps a ridge point whose magnitude reacheshigh, or reacheslowand connects to such a point. The limits are fractions from 0 to 1.
Other filters of the same kind are guided_filter, convolve with your own kernel, and sobel and scharr for the gradients.
The edge
Near the border a filter reaches for pixels that do not exist. edge says what to read there: clamp (the nearest edge value repeats, the default), reflect, mirror, wrap or zero. The last line of the output compares two choices.
API at a glance
| Gaussian blur | stb.gaussian_blur(img, sigma, edge=) |
|---|---|
| Box blur | stb.box_blur(img, radius) |
| Median | stb.median_filter(img, radius) |
| Bilateral | stb.bilateral_filter(img, radius, sigma_space, sigma_range) |
| Unsharp mask | stb.unsharp_mask(img, sigma, amount, threshold) |
| Pixelize | stb.pixelize(img, block_w, block_h) |
| Canny | stb.canny(img, low, high, sigma) |
| Edge mode | edge="mirror" |
| In place | stb.unsharp_mask_inplace(img, ...) |