Gimp-Forum.net
Learn to create Python3 plugins - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: Learn to create Python3 plugins (/Thread-Learn-to-create-Python3-plugins)

Pages: 1 2


Learn to create Python3 plugins - z-uo - 12-27-2021

I want to learn to create Python3 plugins for GIMP. And when I search I do not find much examples on the web so I ask here. The main thing that I want to do (and document on this thread is):
  1. goat-exercise tutorial and learn
  2. port a plugin from python2 to python3 (maybe I will port gimp-balloon-plugin)
  3. create AI based plugins using torch AI like GIMP-ML do with Opencv. For example some interesting features are:
  • OCR: to recognize and maybe translate some text inside images
  • Super resolution: increase the resolution with a deep algorithm
  • Inpainting: redraw some removed areas automatically
I start from the point 1:
  • GIMP 2.99.8 installed with flatpack on Opnesuse Tumbleweed
  • I put goat-exercise into ~/.config/GIMP/2.99/plug-ins/python
  • Restart Gimp
  • I do not see nothing new into Filters > Development > Goat exercises
I miss some configurations? What should I see in there?


RE: Learn to create Python3 plugins - programmer_ceds - 12-27-2021

(12-27-2021, 09:59 AM)z-uo Wrote: I want to learn to create Python3 plugins for GIMP. And when I search I do not find much examples on the web so I ask here. The main thing that I want to do (and document on this thread is):
  1. goat-exercise tutorial and learn
  2. port a plugin from python2 to python3 (maybe I will port gimp-balloon-plugin)
  3. create AI based plugins using torch AI like GIMP-ML do with Opencv. For example some interesting features are:
  • OCR: to recognize and maybe translate some text inside images
  • Super resolution: increase the resolution with a deep algorithm
  • Inpainting: redraw some removed areas automatically
I start from the point 1:
  • GIMP 2.99.8 installed with flatpack on Opnesuse Tumbleweed
  • I put goat-exercise into ~/.config/GIMP/2.99/plug-ins/python
  • Restart Gimp
  • I do not see nothing new into Filters > Development > Goat exercises
I miss some configurations? What should I see in there?

Firstly if your Python plug-in is called myplugin.py (for example) then you have to put this into a folder called myplugin and put the folder into the user plug-ins folder. On my system with the flatpak version this is:

/home/richard/.var/app/org.gimp.GIMP/config/GIMP/2.99/plug-ins

On Linux based systems you also need to make Python scripts executable otherwise they will not appear in the menus.

Also check for any obvious errors by running the script in a terminal window for example:
./myplugin

this should report any obvious problems of syntax or indentation.

Then when testing your script run GIMP from a terminal window - you may see error messages that relate to your script.

For debugging you can use print statements in your script - run GIMP from a terminal window and the output will be seen in the terminal window.

For a few examples of Python 3 scripts the easiest thing may be to download the GIMP V2.99 sources and then look in:

.../GIMP 2_99/gimp-master/plug-ins/python

(obviously the first part of the path will depend on where you have unzipped the sources)


RE: Learn to create Python3 plugins - z-uo - 12-27-2021

(12-27-2021, 01:42 PM)programmer_ceds Wrote:
(12-27-2021, 09:59 AM)z-uo Wrote: I want to learn to create Python3 plugins for GIMP. And when I search I do not find much examples on the web so I ask here. The main thing that I want to do (and document on this thread is):
  1. goat-exercise tutorial and learn
  2. port a plugin from python2 to python3 (maybe I will port gimp-balloon-plugin)
  3. create AI based plugins using torch AI like GIMP-ML do with Opencv. For example some interesting features are:
  • OCR: to recognize and maybe translate some text inside images
  • Super resolution: increase the resolution with a deep algorithm
  • Inpainting: redraw some removed areas automatically
I start from the point 1:
  • GIMP 2.99.8 installed with flatpack on Opnesuse Tumbleweed
  • I put goat-exercise into ~/.config/GIMP/2.99/plug-ins/python
  • Restart Gimp
  • I do not see nothing new into Filters > Development > Goat exercises
I miss some configurations? What should I see in there?

Firstly if your Python plug-in is called myplugin.py (for example) then you have to put this into a folder called myplugin and put the folder into the user plug-ins folder. On my system with the flatpak version this is:

/home/richard/.var/app/org.gimp.GIMP/config/GIMP/2.99/plug-ins

On Linux based systems you also need to make Python scripts executable otherwise they will not appear in the menus.

Also check for any obvious errors by running the script in a terminal window for example:
./myplugin

this should report any obvious problems of syntax or indentation.

Then when testing your script run GIMP from a terminal window - you may see error messages that relate to your script.

For debugging you can use print statements in your script - run GIMP from a terminal window and the output will be seen in the terminal window.

For a few examples of Python 3 scripts the easiest thing may be to download the GIMP V2.99 sources and then look in:

.../GIMP 2_99/gimp-master/plug-ins/python

(obviously the first part of the path will depend on where you have unzipped the sources)

Thank you! I added "~/.config/GIMP/2.99/plug-ins/" to Edit > Preferences > Folders > Plug-Ins, after I put my script called es1.py into the folder es1 and not pythont. I do chmod +x es1.py and now it works! Thank you!


RE: Learn to create Python3 plugins - z-uo - 12-28-2021

Now I passed to the second point: Porting a python2 plugin to python3, but I find a problem on creating a simple transparent layer:
Code:
# ERROR add new trasparent layer
overlay_layer = Gimp.Layer.new(
                       image, 'hide_background',
                       drawable.get_width(), drawable.get_height(),
                       Gimp.ImageType, 100.0,
                       Gimp.LayerMode.NORMAL
)
overlay_layer.fill(Gimp.FillType.TRANSPARENT)
It say that image is not of type Gimp.ImageType:
Code:
File "/home/opensuse/.var/app/org.gimp.GIMP/config/GIMP/2.99/plug-ins/add_baloon/add_baloon.py", line 114, in run
   overlay_layer = Gimp.Layer.new(
TypeError: Expected a Gimp.ImageType, but got type

Why? What I do wrong?
The full code can be find here.


RE: Learn to create Python3 plugins - programmer_ceds - 12-28-2021

(12-28-2021, 11:14 AM)z-uo Wrote: Now I passed to the second point: Porting a python2 plugin to python3, but I find a problem on creating a simple transparent layer:
Code:
# ERROR add new trasparent layer
overlay_layer = Gimp.Layer.new(
                       image, 'hide_background',
                       drawable.get_width(), drawable.get_height(),
                       Gimp.ImageType, 100.0,
                       Gimp.LayerMode.NORMAL
)
overlay_layer.fill(Gimp.FillType.TRANSPARENT)
It say that image is not of type Gimp.ImageType:
Code:
File "/home/opensuse/.var/app/org.gimp.GIMP/config/GIMP/2.99/plug-ins/add_baloon/add_baloon.py", line 114, in run
   overlay_layer = Gimp.Layer.new(
TypeError: Expected a Gimp.ImageType, but got type

Why? What I do wrong?
The full code can be find here.

Code:
# ERROR add new trasparent layer
                    overlay_layer = Gimp.Layer.new(image, 'hide_background',
                        drawable.get_width(), drawable.get_height(),
                        Gimp.ImageType.RGBA_IMAGE, 100.0,
                        Gimp.LayerMode.NORMAL
                    )
                    result = Gimp.get_pdb().run_procedure('gimp-image-get-item-position',
                                 [image,
                                  drawable])

                    position = result.index (1)

                    image.insert_layer(overlay_layer,None,position)
                    overlay_layer.fill(Gimp.FillType.TRANSPARENT)
                    break

Threw me to start with - the problem wasn't with the first parameter (image) but the fifth parameter - the image type.

You also need to insert the new layer - it isn't sufficient to just create it.

The break statement is useful (at the moment anyway) to prevent an endless loop.

There are quite a few places where balloon only has one 'L'

There are also quite a few warnings that are shown in the terminal window that should be addressed.

Happy coding :-)


RE: Learn to create Python3 plugins - z-uo - 12-28-2021

(12-28-2021, 05:38 PM)programmer_ceds Wrote:
(12-28-2021, 11:14 AM)z-uo Wrote: Now I passed to the second point: Porting a python2 plugin to python3, but I find a problem on creating a simple transparent layer:
Code:
# ERROR add new trasparent layer
overlay_layer = Gimp.Layer.new(
                       image, 'hide_background',
                       drawable.get_width(), drawable.get_height(),
                       Gimp.ImageType, 100.0,
                       Gimp.LayerMode.NORMAL
)
overlay_layer.fill(Gimp.FillType.TRANSPARENT)
It say that image is not of type Gimp.ImageType:
Code:
File "/home/opensuse/.var/app/org.gimp.GIMP/config/GIMP/2.99/plug-ins/add_baloon/add_baloon.py", line 114, in run
   overlay_layer = Gimp.Layer.new(
TypeError: Expected a Gimp.ImageType, but got type

Why? What I do wrong?
The full code can be find here.

Code:
                   # ERROR add new trasparent layer
                   overlay_layer = Gimp.Layer.new(image, 'hide_background',
                       drawable.get_width(), drawable.get_height(),
                       Gimp.ImageType.RGBA_IMAGE, 100.0,
                       Gimp.LayerMode.NORMAL
                   )
                   result = Gimp.get_pdb().run_procedure('gimp-image-get-item-position',
                                [image,
                                 drawable])

                   position = result.index (1)

                   image.insert_layer(overlay_layer,None,position)
                   overlay_layer.fill(Gimp.FillType.TRANSPARENT)
                   break

Threw me to start with - the problem wasn't with the first parameter (image) but the fifth parameter - the image type.

You also need to insert the new layer - it isn't sufficient to just create it.

The break statement is useful (at the moment anyway) to prevent an endless loop.

There are quite a few places where balloon only has one 'L'

There are also quite a few warnings that are shown in the terminal window that should be addressed.

Happy coding :-)
Thank you for each correction! I reach to create it!
Now I find that with the following commands I have the selection bounds:
Code:
selection = image.get_selection()
flag, non_empty, x1, y1, x2, y2 = selection.bounds(image)
But how to fill that selection with the PP color (or SF color or White or any color)? Where I can find documentation for the instructions (I only found this that is for python2)?


RE: Learn to create Python3 plugins - programmer_ceds - 12-28-2021

(12-28-2021, 07:27 PM)z-uo Wrote: Thank you for each correction! I reach to create it!
Now I find that with the following commands I have the selection bounds:
Code:
selection = image.get_selection()
flag, non_empty, x1, y1, x2, y2 = selection.bounds(image)
But how to fill that selection with the PP color (or SF color or White or any color)? Where I can find documentation for the instructions (I only found this that is for python2)?
You could use gimp-drawable-edit-fill() - with all of the functions from the GIMP PDB you can call them in the way that I called gimp-image-get-item-position() in the code fragment in my previous post - there may be neater ways to do this for some functions but, as you are finding out, the documentation is somewhat limited. In case you don't know - you can find details on all of the PDB functions in GIMP by going to "Filters/Development/Python-Fu/Python Console/Browse". Type in something related to what you are looking for (for example Layer) and you will all of the functions that contain that word.

Some of this is a bit of an uphill struggle but there is satisfaction when you find the answer :-)


RE: Learn to create Python3 plugins - z-uo - 12-29-2021

(12-28-2021, 10:35 PM)programmer_ceds Wrote:
(12-28-2021, 07:27 PM)z-uo Wrote: Thank you for each correction! I reach to create it!
Now I find that with the following commands I have the selection bounds:
Code:
selection = image.get_selection()
flag, non_empty, x1, y1, x2, y2 = selection.bounds(image)
But how to fill that selection with the PP color (or SF color or White or any color)? Where I can find documentation for the instructions (I only found this that is for python2)?
You could use gimp-drawable-edit-fill() - with all of the functions from the GIMP PDB you can call them in the way that I called gimp-image-get-item-position() in the code fragment in my previous post - there may be neater ways to do this for some functions but, as you are finding out, the documentation is somewhat limited. In case you don't know - you can find details on all of the PDB functions in GIMP by going to "Filters/Development/Python-Fu/Python Console/Browse". Type in something related to what you are looking for (for example Layer) and you will all of the functions that contain that word.

Some of this is a bit of an uphill struggle but there is satisfaction when you find the answer :-)

Thank you! with the Browse and the python function dir(OBJ) and OBD.__dict__ I reach to complete the porting!
Now I try to do an unnecessary thing: I want that at the end a particular layer is the selected(that is the group in witch contains other two created layers created by the plugin) so I try with:
Code:
garray = Gimp.ObjectArray()
garray.data.__add__([layer_group])
Gimp.get_pdb().run_procedure('gimp-image-set-selected-layers',
                                 [image, 1, garray ])
Or simply with:
Code:
Gimp.get_pdb().run_procedure('gimp-image-set-selected-layers',
                                 [image, 1, [layer_group] ])

But it Crashes (the entire GIMP crashes).
The full code can be found here.
How can I select a specific layer by code?


RE: Learn to create Python3 plugins - programmer_ceds - 12-29-2021

(12-29-2021, 05:07 PM)z-uo Wrote: Now I try to do an unnecessary thing: I want that at the end a particular layer is the selected(that is the group in witch contains other two created layers created by the plugin) so I try with:
Code:
garray = Gimp.ObjectArray()
garray.data.__add__([layer_group])
Gimp.get_pdb().run_procedure('gimp-image-set-selected-layers',
                                 [image, 1, garray ])
Or simply with:
Code:
Gimp.get_pdb().run_procedure('gimp-image-set-selected-layers',
                                 [image, 1, [layer_group] ])

But it Crashes (the entire GIMP crashes).
The full code can be found here.
How can I select a specific layer by code?

Look at lines 699 to 720 in this script:

http://programmer97.byethost10.com/Files/CropBetweenGuides.zip


RE: Learn to create Python3 plugins - z-uo - 12-30-2021

(12-29-2021, 06:13 PM)programmer_ceds Wrote:
(12-29-2021, 05:07 PM)z-uo Wrote: Now I try to do an unnecessary thing: I want that at the end a particular layer is the selected(that is the group in witch contains other two created layers created by the plugin) so I try with:
Code:
garray = Gimp.ObjectArray()
garray.data.__add__([layer_group])
Gimp.get_pdb().run_procedure('gimp-image-set-selected-layers',
                                 [image, 1, garray ])
Or simply with:
Code:
Gimp.get_pdb().run_procedure('gimp-image-set-selected-layers',
                                 [image, 1, [layer_group] ])

But it Crashes (the entire GIMP crashes).
The full code can be found here.
How can I select a specific layer by code?

Look at lines 699 to 720 in this script:

http://programmer97.byethost10.com/Files/CropBetweenGuides.zip

Thank you so the working solution to select one (or more layer) is image.set_selected_layers([layer_group]). Now I can continue with point 3!