3

How can I add a plain black line in an image in Gimp in specific pixel coordinates for example between (10,15)->(300,116)?

2 Answers 2

4

Open the Python console in filters->python->console - get a reference to your image, typing

img = gimp.image_list()[0]

and press enter. Hit the browse button at the botton of the dialog and select gimp- paintbrush-default and hit apply - it will paste a template for calling that function in the Python console - something like this:

>>> pdb.gimp_paintbrush_default(drawable, num_strokes, strokes)

Go back with the arrow keys and edit it so that it reads:

>>> pdb.gimp_paintbrush_default(img.layers[0], 4, [10,15,300,116 ])

And press enter. The img.layers[0] parameter is a reference to the layer - "0" is the top most layer in the image, 1, the second from top to bottom, and so on. (Likewise, in the previous command img = gimp.image_list()[0] retrieves a reference to the last image open in GIMP, and changing the number in square bracktes selects other images).

After the first stroke, you can simply hit up arrow, and get the command back, where you can fill in new coordinates for the line. Also, of course, you can explore the procedures available in the "Browse" button, and use any of them you like.

As few people do this, I'v just noticed a bug: in GIMP 2.8, it ignores the brush size set in the tool options. So you will have to create a brush of the size you want and set it before doing the line.

5
  • Hello, have you got any link to the documentation of pdb.gimp_paintbrush? Commented Dec 3, 2021 at 18:45
  • All the documentation that is is what show up in the "pdb Browser". I can expand on what is on this answer: The second parameter is the length of the list given in the third parameter (PDB is C based and designed to be cross-language: it does not know about array lengths, although it is automatic in Python). The third parameter is a list where each 2 numbers are a "x,y" coordinate indicating that coordinate was part of a continuous stroke. So, the example here should draw a straight segment with the current colors, brush, settings and dynamics.
    – jsbueno
    Commented Dec 3, 2021 at 19:38
  • There is no programatic way of including per-stroke pressure, tilt, or other parameters in order to get the dynamics effects to activate.
    – jsbueno
    Commented Dec 3, 2021 at 19:39
  • thanks a lot for the quick response, I am just trying to draw complex shapes from a list of coordinates. I was replicating your example, but I have no control on the width of the line, or the color. So I was looking for the doc of the api to navegate it a little before formulating a question on here. I found this other answer stackoverflow.com/a/35874207/1773841, which uses a similar approach, but it only paint dots, instead of the lines of your example. So I am basically trying to find a way to draw lines, having more control over the lines parameters. Still thanks a lot for the help Commented Dec 3, 2021 at 19:49
  • 1
    You have to set brush size and color on the "context" - look for the "gimp-context-set" methods.
    – jsbueno
    Commented Dec 3, 2021 at 19:57
0

An update for use on Ubuntu 23.04 and the new API:

The python api changed a bit, I experimented around a bit and figured out that now the functions are called as follows:

image = Gimp.list_images()[0]
image_layer = image.list_layers()[-1]
#Gimp.context_set_brush_size(4)
#Gimp.context_set_brush_default_hardness()

Gimp.paintbrush_default(image_layer, [10,15,300,116])

# for text:
Gimp.text_font(image, image.list_layers()[0], pos_x, pos_y, "UL", 0, 1, 50, Gimp.context_get_font())

Why use the new API

I did not manage to install gimp with the "old" python2 console on my machine. The official versions do not seem to ship it anymore (at least through apt, probably because of its python2 depencency?). Therefore I downloaded the latest gimp release (unstable), that uses python 3 as follows:


flatpak install --user https://flathub.org/beta-repo/appstream/org.gimp.GIMP.flatpakref 
flatpak run org.gimp.GIMP//beta
# Execute scripts through:
# Open Filters -> Development -> Python fu -> python console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.