0

I want to manipulate an image with pure NodeJS

I can't find a way how to get the pixel data of an image. I can get the buffer and base64 of the image. I found many libraries that provide this functionality, but I can't reproduce it myself. I thought this was easy to do because in JavaScript we have a Canvas which gives any information about an image and is easy to manipulate

Here is my code:

var fs = require('fs');

fs.readFile('cat.png', (e,image) => {
  console.log(image.toString('base64'));
})

May be here is another way how to read the image?

If this is not possible with pure Node JS, could you please explain to me:

  1. Why?
  2. How do other libraries work with images?
EzioMercer
  • 1,502
  • 2
  • 7
  • 23
  • what do you mean you've found "other libraries"? in npm? if they don't have external software dependencies is has to be achievable by "pure NodeJS". like [this one](https://stackoverflow.com/a/41460508/4935162). Granted, the source code is 20K lines. – Yarin_007 Feb 16 '23 at 14:20
  • I found `sharp`, `jimp`, `png.js` and so on. But can't figure out how they work? – EzioMercer Feb 16 '23 at 14:24
  • Does this answer your question? [Get an array of pixels from an image file using node.js](https://stackoverflow.com/questions/12286039/get-an-array-of-pixels-from-an-image-file-using-node-js) – Peterrabbit Feb 16 '23 at 16:21
  • @Peterrabbit They used libraries and there is answer to any of my questions – EzioMercer Feb 16 '23 at 16:26
  • From a quick overview it seems png.js is by far the simplest. Head over to its [source code](https://www.npmjs.com/package/png-js?activeTab=explore), click on `png-node.js` and check out the `decode` function. – Yarin_007 Feb 16 '23 at 16:48
  • You can do this with pure nodejs but it will be a big amount of complicated code. Libraries like jimp will offer you features similar to what the canvas api does in a browser but coding this yourself makes no sense, except for learning maybe but it's not easy at all. – Peterrabbit Feb 16 '23 at 18:32

0 Answers0