opaque removes all transparency by setting the alpha channel of every pixel to 255.
Use it when you need an image without transparency, or to see the colors hidden in transparent areas.
Signature
image.opaque(): thisopaque 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("sticker.png");
image.opaque();
console.log(image.hasAlpha()); // falsePut a transparent image on a solid background
opaque reveals whatever color the transparent pixels store, which is often black.
To flatten onto a chosen color instead, composite the image onto a new one:
import { Jimp } from "jimp";
const image = await Jimp.read("sticker.png");
const flat = new Jimp({ width: image.width, height: image.height, color: 0xffffffff });flat.composite(image, 0, 0);
await flat.write("sticker-on-white.jpg");Try it
Run the playground to see the result. The dice image stores black in its transparent background, so it turns black.
image.opaque()Set every pixel's alpha to 255. Transparent areas show whatever color they store, often black.
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
- Colors are unchanged. Only alpha changes. Partly transparent pixels show their full stored color, so soft edges can look harsh.
opaqueandhasAlpha. Afteropaque,hasAlphareturnsfalse, and encoders that check for transparency (such as PNG) write the image without an alpha channel.- The opposite. To make an image partly transparent, use
opacityorfade.