11

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

mac
  • 42,153
  • 26
  • 121
  • 131
AsTeR
  • 7,247
  • 14
  • 60
  • 99
  • Start an instance of the GIMP using the `subprocess` module before your script starts to do its work? – Noufal Ibrahim Nov 19 '11 at 18:59
  • possible duplicate of [Connect GIMP with PHP or Python](http://stackoverflow.com/questions/3237252/connect-gimp-with-php-or-python) – unutbu Nov 19 '11 at 22:14
  • 2
    I think, it's not a duplicate since I explicitely ask how to perform it without the usage of plugin. I've to try the subprocess call but I think that'll fail. – AsTeR Nov 20 '11 at 22:26

3 Answers3

15

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.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • 3
    It thought about that approach too. Can you confirm that this is 100% sure there's no way to use GIMP inside Python instead of using Python inside GIMP ? – AsTeR Nov 20 '11 at 22:21
  • 1
    As one of the contributors to that code, I confirm it to you. – jsbueno Nov 21 '11 at 12:20
  • @jsbueno is there any forseeable way for this to be possible someday? – magnetar Aug 25 '12 at 23:23
  • Pay attention to this answer: havign GIMP running in background is not that bad. You can have a Python-plugin that instantiates a xml-rpc server and exchange data with it - there are several ways to do it - you just need to keep a GIMP process running - that is not the end of the world. – jsbueno Aug 30 '12 at 14:55
  • On the other hand, all image capbilities handling in GIMP are implemented (or soom tobe implemented) in the GEGL - Generic Graphics Library. It does feature Python bindings, and require no process to be running - you could try to use thta instead, althouhg GEGL Python maybe a bit rough to get going. – jsbueno Aug 30 '12 at 14:56
  • 1
    I have found that you need to add a -b '(gimp-quit 1)' or it just sits there after completion. If you don't want to register your script within GIMP, you can just run the python directly with this command line: >>> gimp -i --batch-interpreter=python-fu-eval -b 'execfile("myfilename.py"); pdb.gimp_quit(1)' – greggo Jan 18 '13 at 16:57
0

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.

  • GIMP can be launched by command line without UI. It works fine and you can put your script in it to listen for jobs. – AsTeR Jun 20 '13 at 09:00
-3

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.

Karlo Smid
  • 525
  • 1
  • 5
  • 13
  • 1
    Did you try some "basic workflow" : image loading + manipulation + save ? @jsbueno tells something else and is a contributor – AsTeR Jun 25 '12 at 07:45
  • 3
    After I tried basic workflow with calling pdb functions I got same error. Adding gimp to python path is not the solution for running pdb functions without gimp. My mistake. – Karlo Smid Jun 27 '12 at 19:27
  • 2
    You can import the modules, but without a running GIMP, it does not work - instead, Python terminates with "LibGimpBase-ERROR **: gimp_wire_write_msg: the wire protocol has not been initialized " – jsbueno Aug 30 '12 at 14:53