photon rust 图像处理库
photon 是一个基于rust 开发的图像处理库,同时也支持基于WebAssembly的处理
参考nodejs 使用
- 添加依赖
{
"name": "image-demo",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@silvia-odwyer/photon-node": "^0.3.3"
}
}
- 代码使用
一个图像灰度处理的
var fs = require('fs');
var photon = require("@silvia-odwyer/photon-node");
function grayscaleImage() {
// read file, then convert to base64
var base64 = fs.readFileSync(`input.png`, { encoding: 'base64' });
let data = base64.replace(/^data:image\/(png|jpg);base64,/, "");
// convert base64 to PhotonImage
var phtn_img = photon.PhotonImage.new_from_base64(data);
photon.grayscale(phtn_img);
// get base64 from filtered image, and write
let output_base64 = phtn_img.get_base64();
let output_image_name = "output.png";
var output_data = output_base64.replace(/^data:image\/\w+;base64,/, '');
fs.writeFile(output_image_name, output_data, {encoding: 'base64'}, function(err) {
});
console.log(`Saved ${output_image_name}`);
}
grayscaleImage();
说明
photon node 包是基于了wasm-pack 开发的
参考资料
https://github.com/silvia-odwyer/photon
https://github.com/silvia-odwyer/photon/blob/master/crate/src/lib.rs
https://silvia-odwyer.github.io/photon/
https://github.com/rustwasm/wasm-pack
https://silvia-odwyer.github.io/photon/guide/using-photon-node/