[英]Using spawnSync to invoke ImageMagick's `convert` with arguments
I'm trying to use spawnSync()
to invoke ImageMagick's convert
command with the necessary options.我正在尝试使用
spawnSync()
来调用带有必要选项的 ImageMagick 的convert
命令。 I can run the command fine directly:我可以直接运行命令:
$ ls testimage.*
testimage.png
$ convert testimage.png -define dds:mipmaps=99 -define dds:compression=dxt5 testimage.dds
$ ls testimage.*
testimage.dds testimage.png
However, if I supply those arguments in an array to spawnSync()
, I get an error:但是,如果我将数组中的 arguments 提供给
spawnSync()
,则会收到错误消息:
> const {spawnSync} = require('child_process')
undefined
> const args = ['testimage.png', '-define dds:mipmaps=99', '-define dds:compression=dxt5', 'testimage.dds']
undefined
> spawnSync('convert', args, {encoding:'utf-8'}).stderr
"convert-im6.q16: unrecognized option `-define dds:mipmaps=99' @ error/convert.c/ConvertImageCommand/1410.\n"
If I join all the arguments into a single argument, convert
shows its usage, so it doesn't like that, either:如果我将所有 arguments 连接到一个参数中,
convert
会显示它的用法,所以它也不喜欢那样:
> spawnSync('convert', [args.join(' ')], {encoding:'utf-8'}).stdout
'Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org\n' +
'Copyright: © 1999-2019 ImageMagick Studio LLC\n' +
'License: https://imagemagick.org/script/license.php\n' +
'Features: Cipher DPC Modules OpenMP \n' +
'Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff webp wmf x xml zlib\n' +
'Usage: convert-im6.q16 [options ...] file [ [options ...] file ...] [options ...] file\n' +
...
How can I pass my sanitized arguments along in a way that will make it work as though I typed the command line shown above?我怎样才能传递经过清理的 arguments,让它像我输入上面显示的命令行一样工作?
Apparently -define
and dds:mipmaps=99
are separate arguments. This works as desired:显然
-define
和dds:mipmaps=99
是分开的 arguments。这按需要工作:
const {spawnSync} = require('child_process')
const args = [
'testimage.png',
'-define',
'dds:mipmaps=99',
'-define',
'dds:compression=dxt5',
'testimage.dds'
]
spawnSync('convert', args, {encoding:'utf-8'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.