Search code examples
pythonwindowsgimpgimpfu

Python and Gimp: How to properly kill the gimp process?


I'm using a python script which is invoked by the photo software gimp to convert pdf to jpg. So far, the script works fine, but when it is finished, a cmd window is left open by gimp saying "press any key to exit". This cmd window is the gimp.exe process and i cannot manage to kill it with my script (i don't want to enter user input everytime i run my script).

I tried python commands like os.system("taskkill /im gimp-2.8.exe") and sys.exit(0) but none of them works.

This is my python script:

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

This is how i invoke my gimp script from windows command line:

"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)"

I would have thought that gimp command gimp_quit(1) would close the windows but it does not.

It seems like such a simple issue, but i've been spending hours on this so any help appreciated. Thanks.


Solution

  • Ok, i got the solution by asking in a gimp specific forum:

    The extra console window was opened because i did not specify were gimp should log it's messages. That's why it has opened an extra console window.

    So the request now looks like this:

    "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