How to Run Python Scripts Using Gimpfu from Command Line

How to run python scripts using gimpfu from command line?

To run a python script you don't need to have it register as a plugin. IMHO you should even avoid this, this unnecessarily pollutes Gimp's menus and procedure name space. Here is an example:

The batch script (saved as batch.py):

#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

import os, glob, sys, time
from gimpfu import *

def process(infile):
print "Processing file %s " % infile
image = pdb.gimp_file_load(infile, infile, run_mode=RUN_NONINTERACTIVE)
drawable = image.active_layer

print "File %s loaded OK" % infile
pdb.plug_in_photocopy(image, drawable,8.,0.8,0.2,0.2)
pdb.plug_in_cartoon(image, drawable, 7.,0.2)
outfile=os.path.join('processed',os.path.basename(infile))
outfile=os.path.join(os.path.dirname(infile),outfile)
print "Saving to %s" % outfile
pdb.file_jpeg_save(image, drawable, outfile, outfile, "0.5",0,1,0,"",0,1,0,0)
print "Saved to %s" % outfile
pdb.gimp_image_delete(image)

def run(directory):
start=time.time()
print "Running on directory \"%s\"" % directory
# os.mkdir(os.path.join(directory,'processed'))
for infile in glob.glob(os.path.join(directory, '*.jpg')):
process(infile)
end=time.time()
print "Finished, total processing time: %.2f seconds" % (end-start)

if __name__ == "__main__":
print "Running as __main__ with args: %s" % sys.argv

To call it:

gimp -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"

The parameters in slow-motion:

  • -idf: work without user interface, and load neither data nor fonts (you may perhaps need to keep the fonts to load pdfs)
  • --batch-interpreter python-fu-eval: whatever follows -b is Python, not script-fu
  • "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')": the code that we ask Gimp to execute, namely:
    • import sys;sys.path=['.']+sys.path;: extend the import path to include the current directory
    • import batch;: import the file with our script, which is now in a directory which is part of the path.
    • batch.run('./images'): call the run() function of the batch module we imported, giving it the name of a directory containing the images to process.
  • -b "pdb.gimp_quit(1)": another piece of python: exit when done.

Note how the command line cleverly uses double and single quotes to pass all parameters to Gimp and then to Python(*). And yes, you can use forward slashes as file separators in Windows.

To debug, matters are bit complicated in Windows because there is no always a stdout stream. Things that can help:

  • remove the -i parameter temporarily so that you get the UI and perhaps a chance to see messages.
  • Add --verbose which makes Gimp start a secondary console window.
  • There are other tricks to see messages listed here.
  • You can also start Gimp normally and run your script from the Python-fu console (Filters>Python-fu>Console). You will have to extend the path and import the file "manually".

(*) In a Linux/OSX shell one would do the opposite: single quotes for the shell, double quotes for Python.

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

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

Running python GIMP

In Windows, the simplest way is to enclose the CMD.EXE token in double quotes, and use single quotes for the Python code(*):

gimp-console-2.8.exe -df --batch-interpreter python-fu-eval -b "from gimpfu import *;pdb.python_fu_black_and_white(0,'L:/PICS', 'L:/OUT')" -b "pdb.gimp_quit(1)"

You can avoid plenty of problems by using forward slashes as file separators, Windows accepts them everywhere except in command line parameters (but here they are in Python strings).

Also, you don't need to register your script as a plugin if you use it only in batch. See here for some sample code.

(*) In Linux/macOS, do the opposite: single quotes around command line parms, double quotes for Python strings.

Can't run Gimp batch commands from a Mac / OSX

You need to make your alias /Applications/GIMP-2.10.app/Contents/MacOS/gimp. That's where the traditional executables live. Or, make a symbolic link in /usr/local/bin.

Gimp Python script not showing in menu

Works for me, so the only reasons I can think of is that either:

  • the file isn't in one of the directories scanned by Gimp (Edit>Preferences>Folders>Plugins)
  • you didn't set the file as executable (chmod +x ...). On OSX and Linux, Gimp only scans for files marked as executable in the plugins directories.

In the general case if your plugin doesn't register:

  1. Before even adding it to Gimp, do a python pluginfile.py in a terminal, blatant errors will show there. When it complains about gimpfu you are good to go.
  2. Add some visible marker lines print '*************************' at the top of your main, this will be executed each time the plugin runs and make it easier to spot other output from your script. You can also use more informative print lines.
  3. Start Gimp in a terminal. If you don't see the marker line then your code is likely not run at all: is it a directory scanned by Gimp? Is is executable?
  4. Look for runtime error messages and fix them.

Executing Python GIMP scripts on Mac OS

gimpfu is only available when the script is launched from Gimp. There are two cases:

  • The script is a regular plugin, it should contains at least a call to register(...) and a call to main(). This identifies it to Gimp and it can be called from the Gimp UI. Note that the best place to keep it in the plug-ins directory in your Gimp profile, and not the Gimp installation directories. Since you are on OSX, I recommend that you keep the Python file in a directory that you can access easily (and that you backup) and add a soft link to it in your Gimp profile.

  • The script is just a script to be used in batch mode, it doesn't need to be in the plug-ins directory, it must still be called from Gimp which is done by calling Gimp and passing the script as parameters, with a command line such as:

     gimp -idf --batch-interpreter python-fu-eval -b 'import sys; sys.path=["."]+sys.path;import batch;batch.run("./images")' -b 'pdb.gimp_quit(1)'

Where:

  • the Python path is extended to include the directory from which the Python file will be loaded: sys.path=["."]+sys.path (here the current directory is used). You can possibly extend the general Python path permanently instead.
  • the script is added to Python: import batch (script is assumed to be in ./batch.py)
  • the run() method in that file is called with any pertinent parameters

Mind the single and double quotes, some for Python, some for your shell interpreter.

Note that calling the script directly from the command line as you did has some merit, many (but not all...) syntax errors are caught at this point, and you don't have to wait for Gimp to start to find these out.



Related Topics



Leave a reply



Submit