6

I've been trying to switch from script-fu to python-fu, and I can't figure out what I'm supposed to pass for the "image" parameter. In script-fu it was just an integer, but when I put the same integer into python-fu, it just says it's the wrong type. Same with strings and floats... what am I supposed to put in here? The documentation just says the parameter it takes is "IMAGE" but what does that mean? Where do I find it? How do I get it?

pdb.gimp_image_get_layers(image)

Here's a picture of the interpreter.

bignose
  • 30,281
  • 14
  • 77
  • 110
Zaay'd
  • 65
  • 1
  • 5

1 Answers1

12

You pass an Image object - Most of the parameters that are only integers in script-fu are not to be used - in their place, you have to pass an actual object reference. ANd how do you get those?

Simply all PDB functions and methods return these objects already - not their numeric IDs, like it happens in script-fu.

So, for Image, you either get the Image as parameter for your python-fu function, or on a call to pdb.gimp_image_new.

If, as it is often the case, you are making tests on the interactive console, you have to get references to the active images. In that case, call gimp.image_list() to get a Python list with the currently open images - the image at index 0 on this list is the rightmost (newest) open image on the screen -so just do image = gimp.image_list()[0] to get a reference to it.

While you are at it, explore the image object with dir(image) you willfind it is populated with attributes and methods that are handy shortcuts to otherwise hard-to-type pdb calls. For example, image.layers gives you a Python list with reference to all layers on the image (as actual Layer objects, not their IDs), image.width gives you the width, and calling img.new_layer() creates a new layer and adds it to the image - this call has optional parameters to specify its name, width, height and so on.

As a last resort, there are reserved methods on the gimp module that can convert a numeric ID to the actual objects: '_id2display', '_id2drawable', '_id2image', '_id2vectors' - you should not need to use those. If you ever find your self needing to use those in the body of a script, due to some pdb function returning IDs instead of objects, please fill a bug report on GIMP's bugzilla.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • I'm testing this answer with 2.10 and can't use image.width or image.layers using this technique. "image" is an int (type(image)). Are there changes in API in 2.10? – Mario Mey Oct 21 '18 at 23:17
  • WHat exact version are you using? `gimp.list_images()` should return Image objects, not int's. It would be a major regression - but not impossible to happen - and would require waiting a new minor version for a fix. – jsbueno Oct 22 '18 at 00:55
  • I just opened GIMP, and `gimp.list_images()` now returns me the list of images... and each image has `.layers`. I don't know what I had done when I commented. My mistake, sorry about that. – Mario Mey Oct 23 '18 at 02:28
  • What if you have more than one image opened ? how do you get the active one ? It seems you can get it as a function parameter if your code is called as a filter, but what if you run it manually from the console ? – Camion Jun 04 '19 at 21:58
  • `gimp.image_list()` returns a plain Python list with all currently open images. These are rich objects on the Python side and have attributes such as ".name" - so you can check what is your desired image position. ANd if you create a fill plug-in, the program passes the active image automatically as a parameter to your plug-in function. – jsbueno Jun 04 '19 at 22:42