sharpen()
Sharpen edges with an unsharp mask, with strength, radius and threshold.
From @jimp/plugin-blur
sharpen makes edges crisper with an unsharp mask: it compares each pixel with a blurred copy of the image and pushes it further away from the blur.
Edges gain contrast while flat areas stay as they are. Use it after shrinking a photo, or to bring back detail in a slightly soft image.
Signature
image.sharpen(options: { amount: number; radius?: number; threshold?: number;}): thissharpen changes the image in place and returns it, so you can chain more calls after it.
The options type is exported from @jimp/plugin-blur as SharpenOptions.
Options
| Option | Type | Default | Description |
|---|---|---|---|
amount |
number |
required | The strength. 0 changes nothing; 0.5 to 1.5 suits most photos. Must be 0 or more. |
radius |
number |
1 |
The standard deviation of the blur, in pixels, as in gaussian. Must be greater than 0; fractions work. |
threshold |
number |
0 |
Leave a channel unchanged when it differs from the blurred image by less than this, from 0 to 255. Raise it to avoid sharpening noise. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
image.sharpen({ amount: 1 });
await image.write("photo-sharp.jpg");Sharpen after resizing
Shrinking a photo softens it. A light sharpen with a small radius brings back the fine detail:
image.resize({ w: 800 }).sharpen({ amount: 0.6, radius: 0.8 });Leave noise and smooth areas alone
A threshold skips small differences, so grain in skies and skin isn’t sharpened along with the edges. A larger radius affects a wider band around each edge:
image.sharpen({ amount: 0.8, radius: 2, threshold: 4 });Try it
Raise amount to strengthen the effect, radius to widen it, and threshold to stop it from sharpening low-contrast detail.
image.sharpen({ amount: 1.5 })Sharpen edges with an unsharp mask. amount sets the strength, radius the width of the blur it compares with, and threshold skips small differences.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- How it works. Each red, green and blue value becomes
original + amount × (original - blurred), whereblurredis agaussianblur of the image with standard deviationradius. Results are rounded and clamped to0–255. - Halos. Strong settings brighten the light side of an edge and darken the dark side, which shows as a halo. Lower
amountorradiusif you see one. - Transparency is handled. The blurred colors are weighted by alpha, so transparent pixels around a shape don’t darken its edges. Alpha itself isn’t changed, and fully transparent pixels are skipped.
- Matching ImageMagick.
sharpen({ amount, radius, threshold })matchesmagick -unsharp 0x{radius}+{amount}+{threshold × 2 / 255}to within a couple of levels. - Validation. The options are checked with zod. Leaving out
amount, a negativeamount, aradiusof0or less, or athresholdoutside0–255throws a validation error. - Other sharpening kernels. For a fixed 3×3 sharpening kernel, use
convolution. To soften an image instead, useblurorgaussian. - Added in 2.0.