14

What I want to do is to open gimp from a python program (with subprocess.Popen, perhaps), and in the same time, gimp will start with a python script that will open an image and add a layer... Well, how can I achieve that(I wish GIMP had better documentation...)?

Update:

I did this: subprocess.Popen(["gimp", "--batch-interpreter" , "python-fu-eval" , "-b" ,"\'import sys; sys.path.append(\"/home/antoni4040\"); import gimpp; from gimpfu import *; gimpp.main()\'"]) ,but, even if the console says "batch command executed successfully", nothing happens...

Update2:

from gimpfu import *

def gimpp():
    g = gimp.pdb
    images = gimp.image_list() 
    my_image = images[0]
    layers = my_image.layers
    new_image = g.gimp_file_load_layer("/home/antoni4040/Έγγραφα/Layout.png")
    my_image.add_layer(new_image)
    new_layer = g.gimp_layers_new(my_image,  1024, 1024, RGBA_IMAGE, "PaintHere", 0, NORMAL_MODE)
    my_image.add_layer(new_layer)

register('GimpSync', "Sync Gimp with Blender", "", "", "", "", "<Image>/SyncWithBlender", '*', [], [], gimpp)

main()   
morten.c
  • 3,414
  • 5
  • 40
  • 45
Antoni4040
  • 2,297
  • 11
  • 44
  • 56
  • Have you done a web search for relevant resources? Just searching for `python-fu` turned up 3 links which would be sufficient to resolve your problem in the first page of results. – Pedro Romano Sep 30 '12 at 16:24
  • OK, I found some tutorials, but how am I going to make the script start with GIMP? – Antoni4040 Sep 30 '12 at 16:44
  • Just out of curiosity, do you really need gimp for what you are doing? Is it something you could accomplish with PIL? It might be worth looking over PIL's docs at http://www.pythonware.com/products/pil/ – David S Sep 30 '12 at 16:56
  • I'm trying to sync Blender with GIMP... I really think I need GIMP for that... :D – Antoni4040 Sep 30 '12 at 16:59
  • This tutorial explains how to run GIMP in batch mode from the command line with Python-fu: [Python-Fu #4 - Using Python-Fu in Gimp Batch Mode](http://www.exp-media.com/content/extending-gimp-python-python-fu-plugins-part-4). – Pedro Romano Sep 30 '12 at 17:27
  • Updated post with some code... – Antoni4040 Sep 30 '12 at 17:56

1 Answers1

13

Ok I finally got it working. I used the GIMP Python scripting to create a gimp plugin which can do a tonne of stuff including the layers you mentioned. Then you can just run gimp from the command line passing arguments to the python gimp script. The article Using Python-Fu in Gimp Batch Mode was an excellent resource for learning how to call gimp plugins from the command line. The example below will load the specified image into gimp, flip it horizontally, save and exit gimp.

flip.py is the gimp plug-in and should be placed in your plug-ins directory which was ~/.gimp-2.6/plug-ins/flip.py in my case.

flip.py

from gimpfu import pdb, main, register, PF_STRING
from gimpenums import ORIENTATION_HORIZONTAL

def flip(file):
    image = pdb.gimp_file_load(file, file)
    drawable = pdb.gimp_image_get_active_layer(image)
    pdb.gimp_image_flip(image, ORIENTATION_HORIZONTAL)
    pdb.gimp_file_save(image, drawable, file, file)
    pdb.gimp_image_delete(image)

args = [(PF_STRING, 'file', 'GlobPattern', '*.*')]
register('python-flip', '', '', '', '', '', '', '', args, [], flip)

main()

from the terminal one could run this:

gimp -i -b '(python-flip RUN-NONINTERACTIVE "/tmp/test.jpg")' -b '(gimp-quit 0)'

or from Windows cmd:

gimp-console.exe -i -b "(python-flip RUN-NONINTERACTIVE """<test.jpg>""")" -b "(gimp-quit 0)"

or you can run the same from a python script using:

from subprocess import check_output
cmd = '(python-flip RUN-NONINTERACTIVE "/tmp/test.jpg")'
output = check_output(['/usr/bin/gimp', '-i', '-b', cmd, '-b', '(gimp-quit 0)'])
print output

I tested both to make sure they work. You should see the image get flipped after each script run.

Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
Marwan Alsabbagh
  • 25,364
  • 9
  • 55
  • 65
  • First of all, when GIMP opens, Blender freezes, and I don't want that... Secondly, I get this: Error: ( : 136596754) eval: unbound variable: python-gimpp (my .py file is called gimpp...). – Antoni4040 Sep 30 '12 at 19:19
  • did you try the above outside of blender, just to make sure its working. Try it in a terminal and see if it works – Marwan Alsabbagh Sep 30 '12 at 19:35
  • Well, I get this: Error: ( : 155405586) eval: unbound variable: python-flip... It can't find it, I think... – Antoni4040 Oct 01 '12 at 13:44
  • Also, shouldn't the last line be: flip()? – Antoni4040 Oct 01 '12 at 13:46
  • Can you post your exact code and how you run it in Blender so that I can reproduce your error. I have install Blender I'm running Ubuntu – Marwan Alsabbagh Oct 01 '12 at 14:31
  • Well, it seems like the real problem is that the code itself has problems... Updated... – Antoni4040 Oct 01 '12 at 15:13
  • It is very finicky with registration for example I could register with name 'python-flip' but not 'py-flip', and it doesn't throw an error or tell you why. – Marwan Alsabbagh Oct 02 '12 at 04:30
  • So, I should put something like python-GimpSync? Isn't there any other mistake? I'm not sure about the g.gimp_file_load_layer line: if I write it on the console it says that it needs more parameters... – Antoni4040 Oct 02 '12 at 12:32
  • 4
    Using Python-fu link is sadly dead: here's an archive.org link: https://web.archive.org/web/20150328204953/http://www.exp-media.com/content/extending-gimp-python-python-fu-plugins-part-4 – slashdottir Aug 31 '18 at 14:37