gaussian()
Apply a true Gaussian blur. Smoother than blur, but slower.
From @jimp/plugin-blur
gaussian blurs the image with a true Gaussian kernel, so each pixel becomes a weighted average of its neighbors with the nearest ones counting most.
Use it when you need an exact, smooth Gaussian falloff or a fractional radius; for everyday blurring, blur looks almost the same and stays fast at any radius.
Signature
image.gaussian(r: number): thisgaussian changes the image in place and returns it, so you can chain more calls after it.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
r |
number |
required | The blur radius in pixels: the standard deviation of the Gaussian. Any number greater than 0, including fractions. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
image.gaussian(4);
await image.write("photo-soft.jpg");Soften a background behind a subject
Blur a copy of the image and draw the sharp subject back on top of it. Here the subject is a rectangle in the middle:
const subject = { x: 120, y: 80, w: 200, h: 160 };
const sharp = image.clone().crop(subject);
image.gaussian(6).composite(sharp, subject.x, subject.y);Try it
Drag the slider to change r. Unlike blur, fractional radii such as 0.5 or 2.5 work.
image.gaussian(3)A true Gaussian blur. r is the standard deviation in pixels: any value above 0, including fractions. Slower as r grows.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- What
rmeans.ris the standard deviation (sigma) of the Gaussian. Each pixel samples neighbors up toMath.ceil(r * 2.57)pixels away in each direction, sogaussian(4)reaches 11 pixels out. At the samer,gaussianandblurlook about equally soft. - Speed. The blur runs as a horizontal pass then a vertical pass, so the work per pixel grows in step with
r. Small radii are quick; at large radiiblur, whose cost hardly depends on the radius, is much faster. - Transparency is handled. Colors are weighted by their alpha, so transparent areas don’t darken the edges of a shape. Fully transparent results are written as transparent black.
- Edges are extended. Pixels past the border count as copies of the nearest edge pixel.
- Invalid radii. A non-number throws
r must be a number, and0or less throwsr must be greater than 0. Radii below1, such as0.5, give a very light blur. - Changed in 2.0: radii between
0and1used to throw. Anyrgreater than0now works. - Changed in 2.0:
gaussianis a true separable Gaussian blur. It used to read pixels it had already blurred, so its output changes for every image, and colors next to transparent pixels faded toward black. - The whole image.
gaussianhas no region option. To blur part of an image, blur a cropped copy andcompositeit back, as withblur.