gpt4 book ai didi

node.js - Imagemagick/Shell/子进程 NodeJS

转载 作者:太空宇宙 更新时间:2023-11-03 21:51:42 24 4
gpt4 key购买 nike

我似乎无法让 Node 理解我的 Imagemagick 转换命令。它在终端中运行良好,我已经尝试了大量不同的转义字符排列,但它仍然读取我的 args 数组中的每个单独的字符串值,以尝试转换图像:

转换\
输入.jpg\
-写入mpr:XY\
+删除\
-尊重括号\
\( mpr:XY -resize x640 -采样因子 4:2:0 -strip -interlace Plane +write output-640.jpg\)\
\( mpr:XY -resize x320 -采样因子 4:2:0 -strip -interlace Plane +write output-320.jpg\)\
\( mpr:XY -resize x155 -采样因子 4:2:0 -strip -interlace Plane +write output-155.jpg\)\
\( mpr:XY -调整大小 x45 -采样因子 4:2:0 -strip -交错平面 +写入输出-45.jpg\)\
空:

这在 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();
});

关于node.js - Imagemagick/Shell/子进程 NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52354848/

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