What is get-pixels?
The get-pixels npm package is used to read and decode image files into pixel data. It supports various image formats such as PNG, JPEG, GIF, and BMP. This package is useful for applications that need to process or analyze image data at the pixel level.
What are get-pixels's main functionalities?
Load Image from File
This feature allows you to load an image from a file and get its pixel data. The pixel data is returned as a multi-dimensional array.
const getPixels = require('get-pixels');
getPixels('path/to/image.png', function(err, pixels) {
if (err) {
console.log('Failed to load image:', err);
return;
}
console.log('Pixels:', pixels);
});
Load Image from URL
This feature allows you to load an image from a URL and get its pixel data. This is useful for processing images hosted on the web.
const getPixels = require('get-pixels');
getPixels('http://example.com/image.jpg', function(err, pixels) {
if (err) {
console.log('Failed to load image:', err);
return;
}
console.log('Pixels:', pixels);
});
Load Image from Buffer
This feature allows you to load an image from a buffer and get its pixel data. This is useful for processing images that are already loaded into memory.
const getPixels = require('get-pixels');
const fs = require('fs');
const buffer = fs.readFileSync('path/to/image.png');
getPixels(buffer, 'image/png', function(err, pixels) {
if (err) {
console.log('Failed to load image:', err);
return;
}
console.log('Pixels:', pixels);
});
Other packages similar to get-pixels
jimp
Jimp is a powerful image processing library for Node.js that supports reading and writing various image formats, manipulating images, and accessing pixel data. Compared to get-pixels, Jimp offers a broader range of image manipulation features such as resizing, cropping, and applying filters.
sharp
Sharp is a high-performance image processing library for Node.js that supports reading and writing various image formats, resizing, cropping, and converting images. Sharp is known for its speed and efficiency, making it a good choice for high-performance applications. Unlike get-pixels, Sharp focuses more on image transformation and optimization.
pngjs
Pngjs is a pure JavaScript library for reading and writing PNG files. It provides access to pixel data and supports various PNG features such as transparency and color types. While pngjs is limited to PNG files, it offers more control over PNG-specific features compared to get-pixels.
get-pixels
Given a URL/path, grab all the pixels in an image and return the result as an ndarray. Written in 100% JavaScript, works both in browserify and in node.js and has no external native dependencies.
Currently the following file formats are supported:
Example
var getPixels = require("get-pixels")
getPixels("lena.png", function(err, pixels) {
if(err) {
console.log("Bad image path")
return
}
console.log("got pixels", pixels.shape.slice())
})
Install
npm install get-pixels
require("get-pixels")(url[, type], cb(err, pixels))
Reads all the pixels from url into an ndarray.
url
is the path to the file. It can be a relative path, an http url, a data url, or an in-memory Buffer.type
is an optional mime type for the image (required when using a Buffer)cb(err, pixels)
is a callback which gets triggered once the image is loaded.
Returns An ndarray of pixels in raster order having shape equal to [width, height, channels]
.
Note For animated GIFs, a 4D array is returned with shape [numFrames, width, height, 4]
, where each frame is a slice of the final array.
Credits
(c) 2013-2014 Mikola Lysenko. MIT License