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


Messages In This Thread
Basic Tutorial Example - by elindarie - 10-05-2025, 01:02 AM
RE: Basic Tutorial Example - by rich2005 - 10-05-2025, 07:51 AM
RE: Basic Tutorial Example - by elindarie - 10-05-2025, 06:21 PM
RE: Basic Tutorial Example - by rich2005 - 10-05-2025, 06:46 PM

Forum Jump: