简体   繁体   中英

How to Execute Python-Fu script from shell via Gimp

I'm trying to figure out a simple way to execute a Python-Fu script I wrote up that's working in single-threaded mode from the Python-Fu interpreter plugin of Gimp but will no longer work when refactored into its multiprocessing equivalent due to the Windows non POSIX / OS.Fork limitations that pop up in Pool.map_async() . As a work around I'm trying to invoke the script directly from the cmd shell but can't figure out the right command to get it done. I have the gimp bin directory on my PATH variable and am trying to get something achieved like the following...

c:\>gimp-console-2.8 --no-interface --batch "(python-fu execfile('myPyFuScript.py'))"
c:\>gimp-console-2.8 --no-interface --batch "myPyFuScript.py"

Is there a way to get Gimp to execute a python script using its Python-Fu module from a command issued at the Windows cmd shell?

Just for added background I'm taking inspiration from the following documentation ...

GIMP Python Invocation from the Shell

All this means that you could easily invoke a GIMP Python plug-in such as the one above directly from your shell using the (plug-in-script- fu-eval …) evaluator:

gimp --no-interface --batch '(python-fu-console-echo RUN-NONINTERACTIVE "another string" 777 3.1416 (list 1 0 0))' '(gimp-quit 1)'

The string you pass GIMP after --batch is itself executed in script-fu . The easy way to run a Python script there is to write a script-fu expression that will call your python procedure. For example:

gimp  -n --no-interface --batch "(python-fu-gradient-save-as-css  RUN-NONINTERACTIVE \"Sunrise\" \"/tmp/grad.css\")" --batch "(gimp-quit 1)"

Besides the use of the script-fu expression "(python-fu-gradient-save-as-css ... )" you should note the following:

  • the use of -n to ensure a it is run in a new GIMP instance (maybe this is not needed at all - it did work for me here)
  • Use of double quotes ( " ) to delimit string parameters in the script-fu string. Single quotes have different syntactic meaning in scheme/script-fu.
  • the need to call gimp-quit 1 - otherwise, even with --no-interface --batch , GIMP keeps running.

Since you will be calling GIMP from an outher Python script, escaping the " is a non-issue - but people calling it from the command prompt in Windows will have to find a creative way to do that. (Above, they are escaped in bash shell with a \\ )

That should answer your question. However, I will dig a little further: unless the processing you are doing in your script is really heavy-weight, the overhead of calling a separate GIMP instance for each script invocation may be huge (I didn't measure the memory usage - it takes about 2 seconds per invocation on my i5 non SSD machine after the task is properly cached by the OS - so you might as well live with it).

But my approach to it, since performance is an issue, would be to write the GIMP-Python procedure to instantiate a XMLRPC (or JSONRPC ) server and the outer, controlling script to call the "real" procedures through RPC. Using XMLRPC in Python is easy - less than 10 lines boilerplate code you can mostly copy and paste directly from the module docs in http://python.org . That way you could have a pool of GIMO process ready to perform your task, with a simple, fast RPC call (one GIMP per logical core, and you have to put some logic on the controlling script to round-robin/queue the calls, of course).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM