22

Is it possible to get an array of RGB values from a local image file using node.js? I'm trying to write a script that takes a file path as its parameter and returns an array that represents the pixel data.

function getPixelArray(filePath){
    //return an array of RGB values that correspond to the image
}
1

2 Answers 2

29

You can try https://www.npmjs.com/package/jimp This could be useful:

Jimp.read("http://www.example.com/path/to/lenna.jpg", function (err, image) {
    image.getPixelColor(x, y); // returns the colour of that pixel e.g. 0xFFFFFFFF
});

To get RGB you can use:

Jimp.intToRGBA(hex); // e.g. converts 0xFFFFFFFF to {r: 255, g: 255, b: 255, a:255} 
Sign up to request clarification or add additional context in comments.

2 Comments

And use image.bitmap.data to get a Buffer of the raw bitmap data.
Currently there seems to be a bug with the getPixelColor method: github.com/jimp-dev/jimp/issues/1116
8

If your image is in PNG format, have a look at https://github.com/devongovett/png.js/

3 Comments

Is it also possible to save the pixel data as a file (using some other node.js library)? I'd like to modify the array of pixel data, and then save it as a png image again.
Also, is there any way to obtain the image width and height using png-js?
how about without a library?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.