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

131
Views
Cómo convertir imágenes PNG de 24 bits a imágenes PNG de 8 bits

Actualmente estoy usando la biblioteca de lienzos y pude crear con éxito imágenes de 24 bits usando el siguiente código:

 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(); })();

que funciona bien, pero si quiero guardar imágenes como imágenes PNG de 8 bits, y cambiar solo esta línea a:

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

Solo obtengo imágenes en blanco. ¿Qué me estoy perdiendo?

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!