python - 如何不在子流程中引用参数?

标签 python command-line command imagemagick subprocess

我正在尝试使用 subprocess.call 从 Python 2.7 调用 ImageMagick 命令。我的问题是子进程中的参数解析器在每个参数周围放置了双引号,而 ImageMagick 似乎在非文件参数周围使用引号时存在问题。

我想要的是这样的

"imagemagick.exe" "im1.png" "im2.png" -alpha off ( ... ) -composite "im3.png"

到目前为止,除了使用丑陋的 + " " + 手动构造字符串之外,我找不到使用子进程来完成此操作的方法。元素并用 shell=True 调用它.

这是我的代码:

args = [    imagemagick,
            filename + "_b.bmp",
            filename + "_w.bmp",
            "-alpha off ( -clone 0,1 -compose difference -composite -negate ) ( -clone 0,2 +swap -compose divide -composite ) -delete 0,1 +swap -compose Copy_Opacity -composite",
            filename + ".png" ]   

subprocess.call( args )

有没有办法使用子进程调用正确的命令而不使用双引号?

更新:插入完整的命令行。我想将这部分保留在一起,而不是“alpha”,“-off”,......一一。

最佳答案

当使用subprocess调用外部程序时,每个参数必须是args的不同元素。所以尝试一下:

args = [    imagemagick,
            filename + "_b.bmp",
            filename + "_w.bmp",
            "-alpha", "off", "( ... )", "-composite",
            filename + ".png" ]

我不确定 ( ... ) 代表什么,但如果您在命令行上放置不带引号的空格,它们应该是 args 中的单独元素> 也是。

关于python - 如何不在子流程中引用参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11130538/

相关文章:

python - 附加到数据帧引发错误 : cannot concatenate object of type '<class ' tuple'>'; only Series and DataFrame objs are valid

python - 为什么我不能使用 distutils 在 Python 发行版中包含这些数据文件?

python - 如何将 Python 与不同的 SOCKS 5 代理一起使用?

excel - MS Access - Windows 文件夹中的输出数据,可以在程序后删除吗?

compilation - :compiler command do in Vim? 是什么意思

python - 从 Matplotlib DateFormatter 中的一天中的时间删除前导零

python - 使用 Matplotlib 创建多个绘图

python - 如何在Python shell脚本中使用pipenv?

Linux 终端 : Run command when changing directory

python - 在 python 中运行 Fiji 脚本