【发布时间】:2017-06-19 11:07:44
【问题描述】:
我正在使用由照片软件 gimp 调用的 python 脚本将 pdf 转换为 jpg。到目前为止,该脚本运行良好,但完成后,gimp 会打开一个 cmd 窗口,并说“按任意键退出”。这个 cmd 窗口是 gimp.exe 进程,我无法用我的脚本杀死它(我不想每次运行我的脚本时都输入用户输入)。
我尝试了诸如os.system("taskkill /im gimp-2.8.exe") 和sys.exit(0) 之类的python 命令,但它们都不起作用。
这是我的 python 脚本:
import os,time,sys,glob,re
from gimpfu import *
rxcountpages = re.compile(r"/Type\s*/Page([^s]|$)", re.MULTILINE|re.DOTALL)
#Convert a single pdf file
def process(infile, outfile):
print "Processing file %s " % infile
for x in range(1,countPages(infile) + 1):
print "Test"
countStr = str(x)
pdb.file_ps_load_setargs(100, 0, 0, 0, countStr, 6, 2, 2)
image = pdb.file_ps_load(infile,infile)
drawable = pdb.gimp_image_get_active_layer(image)
print "File %s loaded OK" % infile
#outfile=os.path.join('processed',os.path.basename(infile))
#outfile=os.path.join(os.path.dirname(infile),outfile)
print outfile
filename, file_extension = os.path.splitext(outfile)
output = filename + "_" + countStr + ".jpg"
print "Saving to %s" % outfile
pdb.file_jpeg_save(image, drawable, output, output, 0.9,0,1,0,"",0,1,0,0)
print "Saved to %s" % outfile
pdb.gimp_image_delete(image)
print "---------"
def countPages(filename):
data = file(filename,"rb").read()
return len(rxcountpages.findall(data))
if __name__ == "__main__":
print "Running as __main__ with args: %s" % sys.argv
这就是我从 Windows 命令行调用我的 gimp 脚本的方式:
"C:\Program Files\GIMP 2\bin\gimp-2.8.exe" -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import PDF;PDF.process('%1','%2');PDF.exit()" -b "pdb.gimp_quit(1)"
我原以为 gimp 命令gimp_quit(1) 会关闭窗口,但事实并非如此。
这似乎是一个如此简单的问题,但我已经在这方面花费了数小时,所以任何帮助表示赞赏。谢谢。
【问题讨论】:
-
据我记忆,
gimpfu模块有一个.quit()方法。你试过了吗?通过信号杀死 gimp 可能是个坏主意。 -
是的,你是对的,谢谢。我找到了方法
gimp.quit()。但是,如果我将它放在我的脚本中,例如在 for 循环下方,我会收到一条错误消息,提示“python-fu-eval 已关闭且没有返回值”。 -
PDF.exit()的目的是什么?为什么它没有显示在脚本的源代码中? (当我给你的例子中没有这样的东西时,你为什么要添加它?)。您是否尝试将其删除? -
抱歉,我的脚本中缺少该方法,尽管它除了调用
gimp.quit()之外没有做任何其他事情。但是,无论我在 python 脚本中调用 gimp-2.8.exe 或gimp.quit()时尝试使用-b "pdb.gimp_quit(1)",它们都不会关闭 gimp 命令行窗口。
标签: python windows gimp gimpfu