Search code examples
pythonbatch-filecommand-linegimppython-fu

GIMP on Windows - executing a python-fu script from the command line


In a Windows environment, I would like to make a call to GIMP for executing a python-fu script (through a BAT file) but the command line call I am using does not produce the expected results.

For example, consider the following python-fu script named makeafile_and_quit.py, which rerside in my GIMP's plug-ins folder. Its purpose is to load an existing image and save under a different name:

#!/usr/bin/env python

# Sample call from GIMP's python-fu console:
# pdb.python_fu_makeafile_and_quit_script()

from gimpfu import *

def makeafile_and_quit( ) :

    FILEPATH   = 'C:\\path\\to\\file.JPG'
    IMAGE      = pdb.gimp_file_load( FILEPATH,  FILEPATH )

    pdb.gimp_file_save( IMAGE, pdb.gimp_image_get_active_drawable( IMAGE ), FILEPATH + '_2.jpg',  FILEPATH + '_2.jpg' )

    pdb.gimp_quit(0)

    return


# PLUGIN REGISTRATION
# This is the plugin registration function
register(
    'makeafile_and_quit_script',
    'v0.0',
    'A new concept',
    'Author',
    'Author',
    'Just now',
    '<Toolbox>/MyScripts/This will make a file and _QUIT',
    '',
    [],
    [],
    makeafile_and_quit
    )

main()

The script executes flawlessly if called from a 'GUI instance' of GIMP, calling the script through the menus. It produces a new file ending with '_2.jpg' in the same folder as the source file.

The behaviour is different when called from the command prompt using the following:

"C:\Program Files\GIMP 2\bin\gimp-2.8.exe" --batch '("makeafile_and_quit.py")' -b "(gimp-quit 0)"

An instance of GIMP is created, then, closes but no file is created even though the message batch command executed successfully is seen.

How can I repeat exactly the same behaviour as a 'GUI instance', from the command line?


Solution

  • After much fiddling, I arrived at the following command which works as desired:

    "C:\Program Files\GIMP 2\bin\gimp-console-2.8.exe" --verbose --batch "(python-fu-makeafile-and-quit-script RUN-NONINTERACTIVE)" --batch "(gimp-quit 0)"

    Take care to:

    • use gimp-console-2.8.exe instead of gimp-2.8.exe to avoid unnecessary keystroke at the end of execution
    • prefix the function name with python-fu-
    • use -'s instead of _'s in names
    • add the generic (and necessary) RUN-NONINTERACTIVE argument
    • in your script, do not use functions calling displays, such as DISPLAY = gimp.Display( IMAGE ), which make the script fail with gimp-console-2.8.exe