Switch to full style
Ask all general Gimp related questions here
Post a reply

Get a mouse click or select a pixel in an image in a python plugin?

Thu Dec 29, 2022 7:49 pm

GIMP Version: 2.10.32
Operating System: Windows
GIMP Experience: Basic Level



How can a python plug-in get a mouse click position within an image? or... how can a plug-in get have the user select some points within an image? My plug-in needs to prompt the user to designate some points on the image. For example user clicks on a point to select what color is at that pixel, or user clicks on two points to designate the corners of a rectangle, or, even better, user clicks down the mouse an drags it around and the plugin gets sequence of points the mouse was moved in.

The functions I see in libgimp seem to be functions that carry-out the actions like 'set the rectangle selection' to x1,y1 x2,y2' or 'tell me what color is at x,y'. I need to user input side of the operation, not (at this point) the functions to carry-out the operations.

Re: Get a mouse click or select a pixel in an image in a python plugin

Fri Dec 30, 2022 6:16 am

No way to directly access the mouse position in a plugin. Two solutions, either:

  • have the user make a selection, and infer a position form the selection (corners or center)
  • have the user create a path and use the API to get the anchors
The second solution is more general (any number of points or geometry).

Re: Get a mouse click or select a pixel in an image in a python plugin

Fri Dec 30, 2022 4:16 pm

Thanks ScriptCoder. How does something like the Color Picker Tool do it? Is the Color Picker Tool written as an extension, or could Color Picker Tool be written as an extension -- in C or scriptFu or python? Are there functions in some library that cannot be called from python (perhaps using some C/C++ native interface)?

I get the impression that scriptFu and python plug-ins are envisioned as 'scripts' that just invoke a sequence of high level GIMP 'tools'; rather than a means of making general extensions to GIMP functionality (perhaps not as fast as native C code). Is that true?

Re: Get a mouse click or select a pixel in an image in a python plugin

Sat Dec 31, 2022 5:09 am

As you say the Color picker is a "tool"(*). Historically "plugins" do not play with the UI (this is also true for those written in C), only built-in tools can.

You can of course add anything to the Gimp source code and so make your own tools.

(*) which is also used behind the scenes for the "sample points"
Post a reply