greyscale turns the image into shades of gray.
Each pixel’s gray level is based on how bright its color looks to the eye, so greens stay lighter than blues of the same strength.
Signature
image.greyscale(): thisgreyscale takes no arguments. It changes the image in place and returns it, so you can chain more calls after it.
Example
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
image.greyscale();
await image.write("photo-grey.jpg");Black and white with more punch
Raise the contrast after removing the color:
image.greyscale().contrast(0.25);Tinted monochrome
To get a single-color look, remove the color and then mix one back in with color:
image.greyscale().color([{ apply: "mix", params: [{ r: 40, g: 90, b: 200 }, 40] }]);For the classic brown tone, use sepia.
Try it
Run the playground to see the result. Hold the compare button to see the original.
image.greyscale()Remove all color, keeping the perceived brightness of each pixel.
This method has no options.
Loading Jimp…Drop an image to load it
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
- Luminance weights. The gray value is
0.2126 × R + 0.7152 × G + 0.0722 × B(ITU-R BT.709), rounded to the nearest whole number, and written to all three channels. An orange(200, 100, 50)becomes118. - Calling it twice changes nothing. Pixels that are already gray keep their value, so
greyscale()on a gray image leaves it as it is. - Raw values. The weights are applied to the stored values, whatever the image’s color space. Grays, black and white are the same in sRGB and Display P3.
- Alpha is unchanged. Transparent areas stay transparent.
- Spelling. The method is spelled
greyscale; there is nograyscalealias. - The same as the
greyscalecolor action.image.color([{ apply: "greyscale" }])gives exactly the same result, so you can use it inside a longer list ofcoloractions. Thedesaturateaction with100gives a different gray, the HSL lightness. - The image stays RGBA. The pixels are gray, but the bitmap still has four channels. When you write a PNG, Jimp picks a grayscale PNG automatically if every pixel is gray. See Formats.
- Changed in 2.0: the gray value is rounded; before, it was rounded down, so many pixels came out one level darker and calling
greyscale()again could darken them further.