Кажется, я не могу заставить 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:
Это работает в оболочке, отлично работает. Однако этот код полностью не работает
// 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.info(args);
child.stdout.on('data', function(data) {
console.info('stdout: ' + data);
//Here is where the output goes
});
child.stderr.on('data', function(data) {
console.info('stderr: ' + data);
//Here is where the error output goes
});
child.on('close', function(code) {
console.info('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.info(args);
child.stdout.on('data', function(data) {
console.info('stdout: ' + data);
//Here is where the output goes
});
child.stderr.on('data', function(data) {
console.info('stderr: ' + data);
//Here is where the error output goes
});
child.on('close', function(code) {
console.info('closing code: ' + code);
//Here you can get the exit code of the script
process.exit();
});+1 для child.stderr.on('data', function(data) {, реальное место для сбора данных об ошибках, поступающих извне узла, например, кодов python.
двойная косая черта используется для определения следующей строки в терминале. Они вам не понадобятся, когда вы передадите массив аргументов дочернему процессу.