如何从Python脚本启动GIMP命令行?

时间:2017-09-18 15:06:51

标签: python gimp

从这个stackoverflow线程https://stackoverflow.com/questions/4443...mmand-line,我已经解压缩了这个命令行:

gimp-console -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"

效果非常好。

现在,我想从Python脚本运行此命令,通常我使用subprocess.Popen但这次它不起作用我收到此消息:

"batch command experienced an execution error"

如何从Python脚本启动GIMP命令行?

1 个答案:

答案 0 :(得分:-1)

解决此问题的一种简单方法是将您的GIMP启动脚本放入bash脚本中,例如startgimp.sh

#!/bin/bash
#set your path to GIMP or cd into the folder where you installed GIMP
gimp-console -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"

然后从Python中简单地调用bash脚本

import subprocess
subprocess.call(["bash","/path/to/your/script/startgimp.sh"])

如果您能够使.sh脚本可执行,例如chmod +x startgimp.sh然后您可以跳过bash部分并执行subprocess.call("/path/to/your/script/startgimp.sh")

一些警告

  • 假设您使用的是基于UNIX的系统
  • 我使用了subprocess.call,因此在等待GIMP完成时会阻塞。如果你不想要这个,请使用像你一样的Popen
  • 我没有让GIMP尝试这一点,但您也可以尝试将GIMP命令拆分为列表中的元素并将其传递给子流程,看看是否有效。

e.g。 subprocess.call(["gimp-console","-idf","--batch-interpreter","python-fu-eval" and so on)