【问题标题】:Python and Gimp: How to properly kill the gimp process?Python 和 Gimp:如何正确杀死 gimp 进程?
【发布时间】: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


【解决方案1】:

好的,我通过在 gimp 特定论坛中询问得到了解决方案:

打开了额外的控制台窗口,因为我没有指定 gimp 应该记录它的消息。这就是为什么它打开了一个额外的控制台窗口。

所以请求现在看起来像这样:

"C:\Program Files\GIMP 2\bin\gimp-console-2.8.exe" -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import PDF;PDF.process('%1','%2');" -b "pdb.gimp_quit(1)" 1>c:\\temp\\gimp_startup.txt 2>&1

【讨论】:

  • 实际上,最好删除您的打印语句,或者通过调用某些打印或不打印的函数来替换它们,具体取决于某些标志。您还可以使用>NUL 将所有内容发送到位桶。重新路由 stderr 在这里并不是很有用,您将打印输出转到 stdout,而在您遇到真正错误的那一天,您将看不到它们。另外,建议未来的读者,这是一个特定于 Windows 的问题。
猜你喜欢
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
相关资源
最近更新 更多