是否可以使用node.js從本地圖像文件獲取RGB值數組?我試圖編寫一個腳本,它將文件路徑作爲參數,並返回一個表示像素數據的數組。使用node.js獲取圖像文件中的像素數組
function getPixelArray(filePath){
//return an array of RGB values that correspond to the image
}
是否可以使用node.js從本地圖像文件獲取RGB值數組?我試圖編寫一個腳本,它將文件路徑作爲參數,並返回一個表示像素數據的數組。使用node.js獲取圖像文件中的像素數組
function getPixelArray(filePath){
//return an array of RGB values that correspond to the image
}
如果您的圖像是PNG格式,看看https://github.com/devongovett/png.js/
它是PNG格式。謝謝! –
也可以將像素數據保存爲文件(使用其他node.js庫)?我想修改像素數據的數組,然後再次將其保存爲png圖像。 –
另外,有沒有什麼辦法可以使用png-js來獲取圖像的寬度和高度? –
你可以嘗試https://www.npmjs.com/package/jimp 這可能是有用的:
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
});
爲了得到RGB,您可以使用:
Jimp.intToRGBA(hex); // e.g. converts 0xFFFFFFFF to {r: 255, g: 255, b: 255, a:255}
可能的重複:http://stackoverflow.com/questions/11247790/reading-a-pn g-image-in-node-js –