Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Tutorial Example
#1
I don't know if this will help any other beginners on Gimp 3 Python scripting.

Taking most of a day, I made a simple example that works, to my amazement.  I have Gimp 3.0.4 on Ubuntu 22.04, and this runs in GIMP from Filters -> Development -> Python-Fu -> Python Console.

It draws a red box on a blue background.

Code:
import gi
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp

Gimp.context_set_foreground(Gegl.Color.new("red"))
Gimp.context_set_background(Gegl.Color.new("blue"))
image = Gimp.Image.new(300, 400, Gimp.ImageBaseType.RGB)
layer = Gimp.Layer.new(image, "layer 1", 300, 400, Gimp.ImageType.RGBA_IMAGE, 100, Gimp.LayerMode.NORMAL)
image.insert_layer(layer, None, 0)
layer.edit_fill(Gimp.FillType.BACKGROUND)
Gimp.Display.new(image)
Gimp.Image.select_rectangle(image, Gimp.ChannelOps.REPLACE, 60.0, 80.0, 100.0, 150.0)
layer.edit_fill(Gimp.FillType.FOREGROUND)
Gimp.Selection.none(image)
Gimp.displays_flush()

I learned that you can also set colors in this format:

Code:
Gimp.context_set_foreground(Gegl.Color.new("#B00000"))

I found the documentation terribly sparse.  For example, in https://developer.gimp.org/api/3.0/libgimp,

at https://developer.gimp.org/api/3.0/libgi...angle.html, which contains:

Code:
gboolean
gimp_image_select_rectangle (
  GimpImage* image,
  GimpChannelOps operation,
  gdouble x,
  gdouble y,
  gdouble width,
  gdouble height
)

My script has the line: 

Code:
Gimp.Image.select_rectangle(image, Gimp.ChannelOps.REPLACE, 60.0, 80.0, 100.0, 150.0)

How I got from "gimp_image_select_rectangle" to "Gimp.Image.select_rectangle" wasn't clear, and when I looked up the constants for Gimp.ChannelOps.REPLACE,

at https://developer.gimp.org/api/3.0/libgi...elOps.html

it shows GIMP_CHANNEL_OP_REPLACE

from which it wasn't clear to me that this could be Gimp.ChannelOps.REPLACE.

I would be happy to help improve the documentation, which I found sparse and confusing.

I would be grateful if anyone could tell me how to change the dark blue font in the GIMP python console (without changing the Dark Colors scheme), because I couldn't read it at all unless I highlighted it so it showed as reverse video. I couldn't figure out where, maybe in what css file, this is defined.  I'm using gnome and X11.

To do this, it was a lot of hit and miss, and searching for other people's scripts and trying to find similar code in theirs.  Also, I don't really understand how to convert from pdb to non-pdb function calls and back.

I guess my next step is to convert this to a plug-in (.py) file.
Reply
#2
(Yesterday, 01:02 AM)elindarie Wrote: I don't know if this will help any other beginners on Gimp 3 Python scripting.
....snip...
To do this, it was a lot of hit and miss, and searching for other people's scripts and trying to find similar code in theirs.  Also, I don't really understand how to convert from pdb to non-pdb function calls and back.

I guess my next step is to convert this to a plug-in (.py) file.

I am in the same boat, looked at all the available advice, looked at numerous python plugins, left as totally baffled. I hate to say it but script-fu is a little (not much) easier.

It will come with time, there will be documentation that is above "hello world" in the python console but more readable than the present situation.

Anyway, as a dyed-in-the-wool dabbler and for my linear-code requirements I use a simple-ish shell. Your code applies like this, I cannot be bothered to change the registration Wink  The entry point where it goes is after #Create Filter (but filters are a bit of a mystery as well)   FWIW plugin attached.

   


Attached Files
.zip   for-gegl.zip (Size: 1.2 KB / Downloads: 5)
Reply
#3
Thank you, Rich!
Reply
#4
I have to thank you for your example of code. It has helped me.
Reply


Forum Jump: