Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an array of pixels from an image file using node.js

Tags:

node.js

image

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
}
like image 245
Anderson Green Avatar asked Sep 05 '12 16:09

Anderson Green


2 Answers

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} 
like image 61
Michael Yurin Avatar answered Oct 31 '22 08:10

Michael Yurin


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

like image 8
Maciej Avatar answered Oct 31 '22 06:10

Maciej