Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you manipulate an image with Python-Fu (console) in Gimp 3?
#3
(03-22-2025, 11:06 PM)Ofnuts Wrote:
(03-22-2025, 10:26 PM)joeyeroq Wrote: I just installed gimp 3.0 and wanted to test the Python Console.
For example, in Gimp 2.10 you could print the layers of an image with this code in the console:

Code:
img = gimp.image_list()[0]
for layer in img.layers:
    print(layer.name)
How do you do this in Gimp 3?

Things have changed somewhat:

Code:
➤> img=Gimp.get_images()[0]
➤> for l in img.get_layers():
...     print(l.get_name())
...

In practice:
  • The "PDB" thing is pretty much unused. It is only useful to call plugins
  • Most of the code uses object methods directly: for instance, image.get_layers() instead of pdb.gimp_image_get_layers(image)
  • But you no longer have the (rather incomplete) Python "façade" on the PDB (img.layers was such a thing, just a shortcut to pdb.gimp_image_get_layers(image)
  • You have to learn to navigate the doc. To make it very short, 
    • there is a general object-oriented design (OOP), which is why the doc defines classes and such
    • but Gimp is written in C, with no built-in OOP support like you have in Python, so code examples don't look at all like OOP method calls. To make it worse C also mandates some low lever stuff (for instance if you pass an array you have to also explicitly pass its size)
    • but the Python interface re-introduces the OOP,  and removes the low level stuff so you can just pass a list.
  • For instance in the code above, image.get_layers() comes from here, and doesn't look at al like the documented GimpLayer** gimp_image_get_layers (GimpImage* image)(*).

One of these days I'll make a tutorial on how to decode the doc, but I have 50 scripts to convert first...

(*) That coincidentally looks a lot like the 2.10 PDB call...

Is there a difference between script and plugin in gimp 3?
Is it still possible to show a gimp python script in the menu, or does this only work for plugins in Gimp 3?
For example this simple script worked in gimp 2.10:
A file named python-fu-hello-warning.py in folder "C:\Users\USERNAME\AppData\Roaming\GIMP\2.10\plug-ins" with code:

Code:
#!/usr/bin/env python

# Hello Warning 

from gimpfu import *

def hello_warning():
     pdb.gimp_message("hello warning")

register(
    "python_fu_hello_warning",
    "Hello warning",
    "Hello warning TO ERROR CONSOLE",
    "Walter Moore",
    "Walter Moore",
    "2020",
    "Hello warning (Py)...",
    "",
    [],
    [],
    hello_warning, 
    menu="<Image>/File/HelloWarning"
)

main()

Is this still possible in Gimp 3?
Reply


Messages In This Thread
RE: How do you manipulate an image with Python-Fu (console) in Gimp 3? - by joeyeroq - 03-23-2025, 02:36 PM

Forum Jump: