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

237
Views
Converting buffered Image in node.js to pixel data

I'd like to convert a buffered image response, from a fetch request being made, to image data or pixel data, usually stored in a Unit8ClampedArray as demonstrated by the Canvas API. Being that I can convert the buffer to base64 and return a data URI which can be used to view the image, I believe it should be possible to accomplish my goal; however, I couldn't find anything online.

I expected the buffer response to be in the proper order, 4 array items per pixel: rgba; however, as I'm sure you can tell, it's not that easy.

This is the image data I'm referring to:

let imageData = new ImageData(100, 100);
console.log(imageData.data);         // Uint8ClampedArray[40000]
console.log(imageData.data.length);  // 40000

I was missing countless pixels, or perhaps they weren't in the correct order. The outcome was not what I wanted nor was it anything close to the image it was supposed to display.

Here's a piece of my code to help you better understand my troubles:

async drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) {
    image = await RequestHandler.ajax(image, {
        headers: {
            "content-type": "image/png"
        }
    });

    const imageData = new Uint8ClampedArray(image);
    const pixels = [];

    for (let i = 0; i in imageData; i += 4) {
        const average = (imageData[i] + imageData[i + 1] + imageData[i + 2]) / 3;
        if (average < 130) {
            // black;
            pixels.push(0);
        } else if (average < 210) {
            // gray #aaa
            pixels.push(170);
        } else {
            // white
            pixels.push(255);
        }
    }
}
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!