Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Gimp 3.0.6 Python plug-in bug with text layer.set_color(color)?
#1
My environment: Apple Mac mini M2, macOS Tahoe 26.1.

I want to set the color of a text layer from a Python plug-in. In Gimp 2 I used the plug-in "gimp-text-layer-set-color".

In Gimp 3 I find the layer method "set_color(color)", but the color remains to be the foreground color. The same with the PDB plug-in.
First try with layer method:
Code:
textColor = Gegl.Color.new("red") # e.g.
textLayer = Gimp.TextLayer.new(image, "Text", someFont, sometextSize, Gimp.Unit.pixel())
textLayer.set_name("Text")
textLayer.set_color(textColor)
image.insert_layer(textLayer, parent, position)
The text is black.
Next try with pdb proc:
Code:
textColor = Gegl.Color.new("red") # e.g.
textLayer = Gimp.TextLayer.new(image, "Text", someFont, sometextSize, Gimp.Unit.pixel())
textLayer.set_name("Text")
pdb = Gimp.get_pdb()
textLayerSetColor = pdb.lookup_procedure("gimp-text-layer-set-color")
pdbConfig = textLayerSetColor.create_config()
pdbConfig.set_property("layer", textLayer)
pdbConfig.set_property("color", textColor)
textLayerSetColor.run(pdbConfig)
image.insert_layer(textLayer, parent, position)
The text is black.
Next try with foreground:
Code:
textColor = Gegl.Color.new("red") # e.g.
foreground = Gimp.context_get_foreground()
Gimp.context_set_foreground(textColor)
textLayer = Gimp.TextLayer.new(image, "Text", someFont, sometextSize, Gimp.Unit.pixel())
textLayer.set_name("Text")
image.insert_layer(textLayer, parent, position)
Gimp.context_set_foreground(foreground)
Success! The text is red.

Where is the error? With my code or with Gimp?
Reply
#2
Maybe set the color before you use TextLayer.new?
Reply
#3
To change the foreground color I consider as a workaround. Though it works I am still interested in the set_color method. As it is a method of the Gimp.TextLayer class I first need a class instance. I don‘t think I can change the order.
Code:
textColor = Gegl.Color.new("red") # e.g.
textLayer = Gimp.TextLayer.new(image, "Text", someFont, sometextSize, Gimp.Unit.pixel())
textLayer.set_name("Text")
textLayer.set_color(textColor)
Reply
#4
For ease of use, I've also declared a color like this :


Code:
color = Gimp.color_parse_css("rgb(9%, 55%, 77%)")


or without the "%" for rgb 0-255 values

If I'm not mistaken, the GEGL color methods are linear, which makes things a bit un-intuitive.

(11-18-2025, 02:43 PM)Volker Wrote: My environment: Apple Mac mini M2, macOS Tahoe 26.1.

I want to set the color of a text layer from a Python plug-in. In Gimp 2 I used the plug-in "gimp-text-layer-set-color".

In Gimp 3 I find the layer method "set_color(color)", but the color remains to be the foreground color. The same with the PDB plug-in.
First try with layer method:
Code:
textColor = Gegl.Color.new("red") # e.g.
textLayer = Gimp.TextLayer.new(image, "Text", someFont, sometextSize, Gimp.Unit.pixel())
textLayer.set_name("Text")
textLayer.set_color(textColor)
image.insert_layer(textLayer, parent, position)
The text is black.
Next try with pdb proc:


Where is the error? With my code or with Gimp?

I'm surprised
Code:
myTextLayer.set_color(textColor)

doesn't work.

Did you try it after inserting the text layer into the image ?
Reply
#5
Quote:Did you try it after inserting the text layer into the image ?

Bingo! Great idea. Many thanks.
Reply
#6
My problem is solved.

I had a closer look at my Gimp 2 plug-in and indeed found that I had set the color after inserting the layer. I'm sorry to have overlooked this.

Interesting that to set the name with "textLayer.set_name("My Text")" works before or after inserting the layer.

The working code:
Code:
textColor = Gegl.Color.new("red") # e.g.
textLayer = Gimp.TextLayer.new(image, "Text", someFont, sometextSize, Gimp.Unit.pixel())
image.insert_layer(textLayer, parent, position)
textLayer.set_name("Text")
textLayer.set_color(textColor)
Reply
#7
(11-20-2025, 08:36 AM)Volker Wrote: My problem is solved.

Great! Glad I could help.

I know too well how we can bang our heads for hours trying to make a script work. And for GIMP 3.0 API, the documentation is still sparse and sometimes cryptic, especially for non programmers like me for whom not everything is obvious by just looking at the official doc.

The good thing is that it becomes easier as you go. I've now converted some (unpublished) big python scripts using  calls to GEGL and PDB filters, non-linear curves, totally changed gaussian blur filters, colors, etc... Each step was a headache.

BTW, I'm glad that the new RC release includes a GEGL filter browser, because how in the world where you supposed to know what to enter as "filter" and "abyss-policy" parameters into a simple gaussian blur filter before? :o

In a related news, you might want to know that a list of the new API functions, mostly for the new vector and link layers, but also for text layers, is published [there](https://gitlab.gnome.org/GNOME/gimp/-/bl.../NEWS#L143).
Reply
#8
It feels good to see that I'm not the only one who has to fight those adversities. You say that it's a good thing that it becomes easier by and by. But there is another good thing: a sort of mixture of proudness and relief when you come to the solution you were searching for such a long time.

Let's take it as a challenge.

Thanks for the link.
Reply
#9
(11-21-2025, 06:07 PM)Volker Wrote: It feels good to see that I'm not the only one who has to fight those adversities. You say that it's a good thing that it becomes easier by and by. But there is another good thing: a sort of mixture of proudness and relief when you come to the solution you were searching for such a long time.

Let's take it as a challenge.

Thanks for the link.

Totally agree with you, each problem solved tastes like a victory Big Grin

In some way, I'm glad that I started python scripting with GIMP 2.10. It seems the initial learning curve, although quite hard, was more manageable. I'm not sure I would've had the courage to start from scratch nowadays, at least not until some very good tutorials for beginners come out if that happens.

On the upside, python is now a first class citizen, with no additional installation required, and the full API available. So there is even more incentive to code plugins for it.
Reply


Forum Jump: