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

median()

Remove specks and salt-and-pepper noise with a median filter.

From @jimp/plugin-blur

median replaces each pixel with the median of the square of pixels around it, one channel at a time. A few outliers don’t change a median, so isolated specks, dust, dead pixels and salt-and-pepper noise disappear, while edges stay sharp instead of blurring.

A photo of dice sprinkled with black and white specks
Before: 8% of the pixels are black or white specks
The same photo with every speck removed and the dice edges still sharp
After: image.median(1)

Signature

median.d.ts
image.median(radius: number): this

median changes the image in place and returns it, so you can chain more calls after it.

Parameters

Parameter Type Default Description
radius number required How far the window reaches: it’s a square of 2 × radius + 1 pixels on each side. Greater than 0, rounded to a whole number from 1 to 256.

Example

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("scan.png");
image.median(1);
await image.write("scan-clean.png");

Bigger specks

A speck is removed when it covers less than half the window. median(1) (3×3) removes single pixels, median(2) (5×5) removes specks up to about 3 pixels across:

example.ts
image.median(2);

Larger radii also round off corners and erase lines thinner than the window, which gives photos a painted look.

Try it

The photo below has black and white specks. Hold the compare button to see the original, and raise radius to see the painted look of larger windows.

Try median()
image.median(1)

Replace each pixel with the median of the square around it. Removes specks and salt-and-pepper noise and keeps edges sharp.

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

Notes

  • Matching sharp and ImageMagick. median(radius) uses a window 2 × radius + 1 pixels wide, so median(1) matches sharp’s median(3) and ImageMagick’s -statistic Median 3x3.
  • Grain is a job for bilateral. The grain of a photo taken in low light, or the noise around JPEG edges, isn’t made of outliers, so a median leaves it blotchy. Use bilateral for that, and median for specks.
  • Speed. The window slides along each row with a histogram per channel, so each step adds one column and removes another, and nothing is sorted. The cost grows with radius, not radius². A 1200×630 photo takes about 35–60ms with median(1), 80–90ms with median(5) and 140–160ms with median(10) in Node.js on a laptop.
  • Transparency is handled. Colors are weighted by their alpha, so transparent pixels don’t darken a shape’s edges. Alpha is the median of the alpha values, and fully transparent results are written as transparent black.
  • Edges are extended. Pixels past the border count as copies of the nearest edge pixel, as in blur and gaussian.
  • Invalid radii. A non-number, NaN, 0 or less throws a validation error. Radii above 256 work like 256, as in blur.
  • The whole image. median has no region option. To clean part of an image, filter a cropped copy and composite it back.
  • Added in 2.2.