1

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.

flixe
  • 626
  • 1
  • 11
  • 36
  • From what I remember, the `gimpfu` module has a `.quit()` method. Have you tried it? Killing gimp via a signal is probably a bad idea. – Błażej Michalik Jun 19 '17 at 11:10
  • Yes, you're right, thanks. I have found the method `gimp.quit()`. But if i put it in my script for example below the for loop, i get an error saying "python-fu-eval has been closed without return values". – flixe Jun 19 '17 at 11:37
  • What is the purpose of that `PDF.exit()` ? And why is it not shown in the source code of your script? (and why have you added it when there is no such thing in the examples I gave you?). Did you try to remove it? – xenoid Jun 19 '17 at 12:32
  • Sry, the method is missing in my script, although it is not doing anything else than invoking `gimp.quit()`. However, no matter if i try it with `-b "pdb.gimp_quit(1)"` while calling gimp-2.8.exe or `gimp.quit()` from within my python script, none of them close the gimp command line window. – flixe Jun 19 '17 at 13:15

1 Answers1

3

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
flixe
  • 626
  • 1
  • 11
  • 36
  • Actually it would be even better to either remove your print statements, or replace them by calls to some function that prints or not depending on some flag. You can also send every thing to the bit-bucket using `>NUL`. Rerouting stderr is not really useful here, you print output goes to stdout, and the day you have real errors you won't see them. Also, advice to future readers, this is a Windows-specific problem. – xenoid Jun 19 '17 at 16:45