gpt4 book ai didi

javascript - 如何在 Node.js 中使用图像切片?

转载 作者:行者123 更新时间:2023-11-30 20:16:26 24 4
gpt4 key购买 nike

我尝试使用image-slice使用node js切片成多个部分。

我尝试安装npm i image-to-slicessudo port install caironpm i canvasbrew install pkg -config cairo pango libpng jpeg giflib 但仍然在服务器端 Node.js 上显示错误 require node-canvas

这是我尝试切片图像:

 var imageToSlices = require('image-to-slices');

exports.sliceImage=(request, response)=>{


var lineXArray = [100, 200];
var lineYArray = [100, 200];
var source = './public/images/Bitmap001.png'; // width: 300, height: 300

imageToSlices(source, lineXArray, lineYArray, {
saveToDir: './public/images/bit001.png'
}, function() {
console.log('the source image has been sliced into 9 sections!');
});


}//sliceImage

控制台输出错误:

 Error: Require node-canvas on the server-side Node.js
at ImageClipper.__createImage (/Users/parameshv/pyrky_nodejs/node_modules/image-clipper/lib/clipper.js:456:13)
at ImageClipper.loadImageFromUrl (/Users/parameshv/pyrky_nodejs/node_modules/image-clipper/lib/clipper.js:83:20)
at ImageClipper.image (/Users/parameshv/pyrky_nodejs/node_modules/image-clipper/lib/clipper.js:120:10)
at imageClipper (/Users/parameshv/pyrky_nodejs/node_modules/image-clipper/lib/index.js:36:15)
at ImageToSlices.slice (/Users/parameshv/pyrky_nodejs/node_modules/image-to-slices/lib/image-to-slices.js:212:3)
at ImageToSlicesFactory (/Users/parameshv/pyrky_nodejs/node_modules/image-to-slices/lib/index.js:22:17)
at exports.sliceImage (/Users/parameshv/pyrky_nodejs/app/controllers/ApiController.js:843:1)
at Layer.handle [as handle_request] (/Users/parameshv/pyrky_nodejs/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/parameshv/pyrky_nodejs/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/parameshv/pyrky_nodejs/node_modules/express/lib/router/route.js:112:3)

最佳答案

深入研究代码,我发现您可以通过在配置中使用 clipperOptions 来指定要使用的 canvas

试试这个:

var imageToSlices = require('image-to-slices');
var lineXArray = [100, 200];
var lineYArray = [100, 200];
var source = './public/images/Bitmap001.png'; // width: 300, height: 300

imageToSlices(source, lineXArray, lineYArray, {
saveToDir: '.',
clipperOptions: {
canvas: require('canvas')
}
}, function() {
console.log('the source image has been sliced into 9 sections!');
});

关于javascript - 如何在 Node.js 中使用图像切片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51874809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com