Skip to content
docs
GitHub 4.5M /week v2.2.0
Blur & effects

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

gaussian.d.ts
image.gaussian(r: number): this

gaussian 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

example.ts
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:

example.ts
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.

Try gaussian()
image.gaussian(3)

A true Gaussian blur. r is the standard deviation in pixels: any value above 0, including fractions. Slower as r grows.

3px
Loading Jimp…Drop an image to load it
PNG
index.js
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");

Notes

  • What r means. r is the standard deviation (sigma) of the Gaussian. Each pixel samples neighbors up to Math.ceil(r * 2.57) pixels away in each direction, so gaussian(4) reaches 11 pixels out. At the same r, gaussian and blur look 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 radii blur, 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, and 0 or less throws r must be greater than 0. Radii below 1, such as 0.5, give a very light blur.
  • Changed in 2.0: radii between 0 and 1 used to throw. Any r greater than 0 now works.
  • Changed in 2.0: gaussian is 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. gaussian has no region option. To blur part of an image, blur a cropped copy and composite it back, as with blur.