-->

How to use GIMP inside a Python script?

2019-03-19 08:02发布

问题:

GIMP enables you to make plugin in in Python, what I would like to do is to call GIMP function like I would do inside one of this plugin but this return the following error since GIMP doesn't find any running GIMP Core to use.

LibGimpBase-ERROR **: gimp_wire_write_msg: the wire protocol has not been initialized aborting...

I would like to know if it's possible ? And if yes, how ?

Thanks

回答1:

GIMP's Python extensions need to be run from inside a GIMP instance. If you want to use GIMPś API from Python you have to run a GIMP without a graphical UI (passing the -i parameter from the command line) and running a custom call to the api - with the -b command line parameter - so, you can run your python_fu_do_it program, from the command line calling:

gimp -i -b \(python-fu-do-it \)

Note that this is the only way to get gimp-python extensions running: you have to run it from inside a GIMP process.

In real life, a useful thing to do might be to make your gimp-plugin expose some functions that perform actions on images that you want, and export those through an xmlrpc or jsonrpc server - which is easily done in Python. You then start this "image server" using the method above, and create stand-alone python script which call your gimp-using functions through xmlrpc.



回答2:

One option would be to create a listener process inside of gimp as a script (This might have implications regarding locking up the UI, experimentation will be needed here), then getting it to listen to a beanstalkd work queue. then in your external processes, lodge work requests on the beanstalk queue and beanstalk can then process these orders out-of-process.

With all that said, 99% of the use cases I could imagine you wanting to do this, perhaps ImageMagick would be a more appropriate choice than gimp as its kind of designed for the sort of tasks I imagine you are interested in.



回答3:

This package can get you started. Look into the ".bat" file.

http://gimpforums.com/attachment.php?aid=3142



回答4:

I have to say that following statement is not true:

"GIMP's Python extensions need to be run from inside a GIMP instance."

You do not have to run gimp in order to use its functions that are exposed through python gimpfu API.

In any python program, for linux you just do following:

import sys  
sys.path.append('/usr/lib/gimp/2.0/python/')  
import gimpfu  

where '/usr/lib/gimp/2.0/python/' is path to gimp installation.

Regards, Karlo.



标签: python gimp