Skip to content
docs
GitHub 4.5M /week v2.2.0
Color

invert()

Invert the red, green and blue channels.

From @jimp/plugin-color

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

invert.d.ts
image.invert(): this

invert takes no arguments. It changes the image in place and returns it, so you can chain more calls after it.

Example

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

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

Try invert()
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
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

  • How it’s computed. Each channel c becomes 255 − c. Calling invert twice 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.