hasAlpha tells you whether the image uses transparency: it returns true as soon as it finds a pixel whose alpha is below 255, and false if every pixel is fully opaque.
Use it to pick an output format, or to skip work that only matters for transparent images.
Signature
image.hasAlpha(): booleanhasAlpha only reads the image.
Example
Keep transparency by writing a PNG when the image needs it, and a smaller JPEG when it doesn’t:
import { Jimp } from "jimp";
const image = await Jimp.read("upload.png");
if (image.hasAlpha()) { await image.write("output.png");} else { await image.write("output.jpeg");}New images
A new Jimp() without a color starts fully transparent, so hasAlpha returns true until you fill it:
new Jimp({ width: 10, height: 10 }).hasAlpha(); // truenew Jimp({ width: 10, height: 10, color: 0xffffffff }).hasAlpha(); // falsenew Jimp({ width: 10, height: 10, color: 0xffffff80 }).hasAlpha(); // trueNotes
- It looks at the pixels, not the file. A PNG saved with an alpha channel whose pixels are all opaque returns
false. JPEGs have no alpha channel, so a decoded JPEG always returnsfalse. - It stops early. The alpha bytes are checked in order and the loop ends at the first one that isn’t
255, so images with transparency near the top return quickly. A fully opaque image is checked to the last pixel. - Not every format keeps transparency. PNG and ICO store the alpha channel, but JPEG and BMP are written without transparency, GIF keeps only fully transparent pixels, and TIFF doesn’t keep partly transparent pixels exactly. Before encoding JPEG, BMP, GIF or TIFF, Jimp draws the image over
image.background, which is transparent black for images you read, so transparent areas usually turn black. Setimage.background = 0xffffffffbefore writing to get white instead. See Formats.