invert turns the image into its negative: black becomes white, red becomes cyan and so on.
Use it for negative effects, or to flip a light mask into a dark one.
Signature
image.invert(): thisinvert 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.invert();
await image.write("photo-negative.jpg");Flip a mask
mask treats bright pixels as visible and dark ones as hidden.
Invert a copy of the mask to reveal the opposite area:
const shape = await Jimp.read("shape.png");
image.mask(shape.clone().invert());Try it
Run the playground to see the result. Hold the compare button to see the original.
image.invert()Invert the red, green and blue channels. Alpha is unchanged.
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
- How it’s computed. Each channel
cbecomes255 − c. Callinginverttwice gives back the original image. - Alpha is unchanged. Transparent pixels stay transparent, but their stored color is inverted too, so a transparent black background becomes transparent white. That only shows if you later call
opaque. - Same as XOR with white.
image.color([{ apply: "xor", params: [{ r: 255, g: 255, b: 255 }] }])gives the same result. XOR with other colors inverts only some bits of each channel.