Node.jsでPNGファイルを読み込んで画像のピクセルを取得する簡単な方法はありますか? node-imageのようなものですが、その逆です:)
https://github.com/joyent/node/wiki/modules#wiki-graphicsにリストされているライブラリを調べましたが、それらはトリミングとサイズ変更を提供するコマンド ライン ツールの単純なラッパーか、node-canvas
.
Node.jsでPNGファイルを読み込んで画像のピクセルを取得する簡単な方法はありますか? node-imageのようなものですが、その逆です:)
https://github.com/joyent/node/wiki/modules#wiki-graphicsにリストされているライブラリを調べましたが、それらはトリミングとサイズ変更を提供するコマンド ライン ツールの単純なラッパーか、node-canvas
.
これは、ネイティブの依存関係なしで PNG のデコードとエンコードの両方を行います。
pngjs - ネイティブ依存関係のない Node.js 用の PNG エンコーダー/デコーダー.
PNG の色を反転する例:
var fs = require('fs'),
PNG = require('pngjs').PNG;
fs.createReadStream('in.png')
.pipe(new PNG())
.on('parsed', function() {
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
var idx = (this.width * y + x) << 2;
// invert color
this.data[idx] = 255 - this.data[idx]; // R
this.data[idx+1] = 255 - this.data[idx+1]; // G
this.data[idx+2] = 255 - this.data[idx+2]; // B
// and reduce opacity
this.data[idx+3] = this.data[idx+3] >> 1;
}
}
this.pack().pipe(fs.createWriteStream('out.png'));
});
私は検索に夢中になりそうでしたが、1つ見つけました:
png.js ― canvas 要素または Node.js 用の JS の PNG デコーダー。
var PNG = require('png-js');
var myimage = new PNG('myimage.png');
var width = myimage.width;
var height = myimage.height;
myimage.decode(function (pixels) {
//Pixels is a 1D array containing pixel data
});
純粋なJavaScriptであることに注意してください。ブラウザ <canvas>
とNode.JSの両方で動作します。
width
と以外にもプロパティがあります。このソースheight
を参照してください。
おもう
var myimage = new PNG('myimage.png');
する必要があります
var myimage = new PNG.load('myimage.png');