Imagemagick / Shell /子进程NodeJS
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Imagemagick / Shell /子进程NodeJS相关的知识,希望对你有一定的参考价值。
我似乎无法让Node了解我的Imagemagick转换命令。它在终端中运行良好,我已经尝试了大量不同的排列来转义字符,但它仍然会在我的args数组中读取每个单独的字符串值,以尝试转换图像:
convert
input.jpg
-write mpr:XY
+delete
-respect-parentheses
( mpr:XY -resize x640 -sampling-factor 4:2:0 -strip -interlace Plane +write output-640.jpg )
( mpr:XY -resize x320 -sampling-factor 4:2:0 -strip -interlace Plane +write output-320.jpg )
( mpr:XY -resize x155 -sampling-factor 4:2:0 -strip -interlace Plane +write output-155.jpg )
( mpr:XY -resize x45 -sampling-factor 4:2:0 -strip -interlace Plane +write output-45.jpg )
null:
这在shell中运行正常。但是,这段代码完全失败了
// const spawn = require('child-process-promise').spawn;
var spawn = require('child_process').spawn;
function argsForSize(number) {
return [
'\(',
'mpr:XY',
'-resize',
'x' + number,
'-sampling-factor',
'4:2:0',
'-strip',
'-interlace',
'Plane',
'+write',
'output-' + number +'.jpg',
'\)\'
]
}
let inputPath = '/Users/xconanm/Desktop/imagemagick/test-app/input.jpg';
var args = [
inputPath, '\',
'-write', 'mpr:XY', '\', // resize width to 640
'+delete', '\',
'-respect-parentheses', '\'
];
let extraArgs = argsForSize(640).concat(argsForSize(320)).concat(argsForSize(155)).concat(argsForSize(55));
extraArgs.push('null:');
args = args.concat(extraArgs)
let child = spawn('convert', args)
console.log(args);
child.stdout.on('data', function(data) {
console.log('stdout: ' + data);
//Here is where the output goes
});
child.stderr.on('data', function(data) {
console.log('stderr: ' + data);
//Here is where the error output goes
});
child.on('close', function(code) {
console.log('closing code: ' + code);
//Here you can get the exit code of the script
});
得到它......可能效率低下但是:耸耸肩
// const spawn = require('child-process-promise').spawn;
var spawn = require('child_process').spawn;
function argsForSize(number) {
return [
'-write',
'mpr:XY',
'+delete',
'-respect-parentheses',
'mpr:XY',
'-resize',
'x' + number,
'-sampling-factor',
'4:2:0',
'-strip',
'-interlace',
'Plane',
'-write',
'output-' + number +'.jpg'
]
}
let inputPath = '/Users/xconanm/Desktop/imagemagick/test-app/input.jpg';
var args = [
inputPath
];
let extraArgs = argsForSize(640).concat(argsForSize(320)).concat(argsForSize(155)).concat(argsForSize(55));
extraArgs.push('null:');
args = args.concat(extraArgs)
let child = spawn('convert', args)
console.log(args);
child.stdout.on('data', function(data) {
console.log('stdout: ' + data);
//Here is where the output goes
});
child.stderr.on('data', function(data) {
console.log('stderr: ' + data);
//Here is where the error output goes
});
child.on('close', function(code) {
console.log('closing code: ' + code);
//Here you can get the exit code of the script
process.exit();
});
以上是关于Imagemagick / Shell /子进程NodeJS的主要内容,如果未能解决你的问题,请参考以下文章
从Python中的imagemagick子进程读取输出[重复]