Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
How to convert 24bit PNG images to 8bit PNG images

I am currently using canvas library and I was able to successfully create 24bits images using the code below:

const fs = require("fs");
const { createCanvas, loadImage } = require("canvas");

const canvas = createCanvas(3000, 3000); 
const ctx = canvas.getContext("2d", {alpha: false});

const loadMyImage = async (img) => {
    return new Promise(async (resolve) => {
        const image = await loadImage(`${img}`);
        resolve({ layer: img, loadedImage: image });
    });
};

const drawElement = (_renderObject) => {
    ctx.drawImage(_renderObject.loadedImage, 0, 0, 3000, 3000);
};

const saveImage = (img) => {
  fs.writeFileSync(
    `${img}.png`,
    canvas.toBuffer("image/png")
  );
};

const run = async () => {
            let loadedImg = loadMyImage('purple.png');

            //start drawing image
            await Promise.resolve(loadedImg).then((renderObject) => {
                ctx.clearRect(0, 0, 3000, 3000);
                drawElement(renderObject);
                saveImage(1);
            });
};

(() => {
    run();
})();

which works fine, but if I want to save images as 8bit PNG images, and change only this line to:

const ctx = canvas.getContext("2d", { pixelFormat: 'A8' });

I get only white images. What am I missing?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!