Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Select and bucket fill with python fu
#1
I'm trying to modify a script with the two basic commands: 
1.Select a free selection with a series of points
2.Fill that selection with the foreground at 75% opacity

To do command 1 I use the line:
pdb.gimp_image_select_polygon(image,CHANNEL_OP_REPLACE,3,[width/2.0,height/2.0,x0,y0,x1,y1])

The array at the end is filled with variables I define elsewhere. I try to test this in the python console on a file I've called test.xcf to see how I can expect the syntax to react. The line looks like 

pdb.gimp_image_select_polygon('test.xcf',CHANNEL_OP_REPLACE,3,[640,480,700,600,500,600])

But I get this error :Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: wrong parameter type

What Can I do? I assume I'm calling the image wrong but I'm not sure what to call it
--------------------------------------------------------
For command 2 I'm using:
pdb.gimp_edit_bucket_fill(drawable,BUCKET_FILL_FG,LAYER_MODE_NORMAL_LEGACY,75,0,FALSE,0,0)

I figure I can run this line in the console or in code and it should have the same effect: as long as there's a selection on the page then it'll just fill with the foreground. But when I test the syntax I'm met with:
NameError: name 'drawable' is not defined

So what do I call the selection then? I figure this should be simple but I'm just not sure what words I should use.

Additionally: The modifications I'm making to my script now call numpy in. Will this be an issue if I already have numpy installed on my machine, or do I have to specifically install it onto gimp?
Reply
#2
(04-15-2021, 07:31 PM)bozjaws Wrote: I'm trying to modify a script with the two basic commands: 
1.Select a free selection with a series of points
2.Fill that selection with the foreground at 75% opacity

To do command 1 I use the line:
pdb.gimp_image_select_polygon(image,CHANNEL_OP_REPLACE,3,[width/2.0,height/2.0,x0,y0,x1,y1])

The array at the end is filled with variables I define elsewhere. I try to test this in the python console on a file I've called test.xcf to see how I can expect the syntax to react. The line looks like 

pdb.gimp_image_select_polygon('test.xcf',CHANNEL_OP_REPLACE,3,[640,480,700,600,500,600])

But I get this error :Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: wrong parameter type

What Can I do? I assume I'm calling the image wrong but I'm not sure what to call it
--------------------------------------------------------
For command 2 I'm using:
pdb.gimp_edit_bucket_fill(drawable,BUCKET_FILL_FG,LAYER_MODE_NORMAL_LEGACY,75,0,FALSE,0,0)

I figure I can run this line in the console or in code and it should have the same effect: as long as there's a selection on the page then it'll just fill with the foreground. But when I test the syntax I'm met with:
NameError: name 'drawable' is not defined

So what do I call the selection then? I figure this should be simple but I'm just not sure what words I should use.

Additionally: The modifications I'm making to my script now call numpy in. Will this be an issue if I already have numpy installed on my machine, or do I have to specifically install it onto gimp?

  1. The length specified in the call is the number or items of the array, since you have 3 points, this is 6 coordinates so you should pass 6 and not 3.
  2. The image parameter is a gimp.Image object. If this is a plugin called from a Gimp menu to act on the active image, the image is passed as the first parameter to the plugin function if you use the canonical registration format. If this is a batch, the image is returned by pdb.gimp_file_load(...) or pdb.gimp_xcf_load(...).
  3. drawable is a gimp.Drawable (ie, an object on which you can draw/paint): gimp.Layer or gimp.Channel (a gimp.Channel can be a layer mask or a channel). Again,if this is a plugin called from a Gimp menu to act on the active "drawable", this is usually your second parameter with the canonical registration format. if this is a batch, you can use image.active_layer or image.layers[0]  (top layer).
  4. Unless you know exactly what you are doing (and you don't seem to be there yet Angel ) don't use the *_LEGACY mode.
  5. Since you are on Windows, Gimp comes with its own Python runtime, so to use numpy you have to install it for that runtime. Since you use numpy you are possibly implicitly thinking Python3, but keep in mind that Gimp's python is v2.7 (and you cannot change that to use Python3, you can at best twist Gimp's arm to use another python v2 runtime). I assume you have seen https://www.gimp-forum.net/Thread-Gimp-python-and-numpy
Last if you want to try things on the Python console, start with this:

Code:
image=gimp.image_list()[0] # assumes a single image opened in Gimp
layer=drawable=image.active_layer # defines both "layer" and "drawable"

If you have more that one image, you can retrieve the image by its ID:
  • The ID appears in the image title bar a the "X" part of a X.Y number after the file name (or "Untitled").
  • X is the image ID and Y is the view number, and unless you have several views of the same image, the view number is 0.
  • So if you title bar says 3.0 your image ID is 3.
  • Then to retrieve that image in the Python console:
Code:
image,=[i for i in gimp.image_list() if i.ID==3]  # Comma in "image," isn't a typo it is unpacking
Reply


Forum Jump: