In the new version of GIMP it is possible to use Python3 instead of Python2 it will be available in a stable version with GIMP 3 but we just start to use this feature. In this guide I will explain how to start creating this kind of plugin step by step.
I'm on linux so I install GIMP with flatpack, in particular I do not need the stable but the beta.
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/beta-repo/flathub.flatpakrepo
flatpak install --user https://flathub.org/beta-repo/appstream/org.gimp.GIMP.flatpakref
Now we can run it with
flatpak run org.gimp.GIMP//beta
Check python into GIMP Bash
Now we can check python and install pip (that we need to install eventually python dependencies) with the following:
flatpak run --command=bash org.gimp.GIMP//beta
> python --version
> python -m ensurepip
> python -m pip install -U pip
It is also Possible to open a Python shell inside GIMP environment with:
flatpak run --command=python org.gimp.GIMP//beta
Show and add Plugins folders
Open gimp beta:
flatpak run org.gimp.GIMP//beta and go to Edit > Preferences > Folders > Plug-Ins, there you can see the folder in witch GIMP will search for plugins at the startap. I add the path "~/.config/GIMP/2.99/plug-ins/".
Add plugin to folder
Now I go into the folder and I add a simple plugin that went from goat-exercise example:
chmod +x ex1.py
To have the plugin working you must create a folder with the same name of the script except for .py and into that folder you must put the script with execution permissions.
For other plugin examples you can seen the plug-ins/python folder.
Run the plugin
Now restart Gimp, go to File>new and click ok. Go into Filters>Development>Goat Exercise you can find the new entry "Exercise 1 python" click on it and you will see the source code. If you click OK the filter will be applied to the layer.
Found instructions that we need
At first I recommend to see some code examples.
For now the documentation is not ready but we can search the function by opening Gimp, going into Filters>Development>Python-Fu>Python Console and click Browse, here you can search functions and you can find it, with a explanation about the input:
To use that layer you can code in this way:
from gi.repository import Gimpresult = Gimp.get_pdb().run_procedure(
[image, text, font_name, font_size, 3]
layer = result.index(1)
You can also get help by using OBJECT.__dict__ and dir(OBJECT) into the console:
With OBJECT.__dict__ we obtains the field of the object and with dir(OBJECT) we can obtain his methods.
This method is a bit tricky but you can also ask into the gimp_forum if you are in trouble. I think that in the future when Gimp 3.0 will be ready more documentation will be found.
Build UI Interface
You can create the GTK UI programmatically like in the example plugin, but you can also use Glade UI Builder and after you can load it with following code:
GimpUi.init("plugin.py") dialog = GimpUi.Dialog(use_header_bar=True, title=_("plugin"), role="plugin-Python3")
dialog.add_button(_("_Cancel"), Gtk.ResponseType.CANCEL) dialog.add_button(_("_OK"), Gtk.ResponseType.OK)
builder = Gtk.Builder() dir_path = os.path.dirname(os.path.realpath(__file__)) builder.add_from_file(os.path.join(dir_path, "ui.glade"))
box = builder.get_object("box") dialog.get_content_area().add(box) box.show()
For now during developing I only open with an editor my plugin and I edit it, gimp should not be restarted if you save the plugin and call it by Gimp it have the fresh code automatically.
Here you can find an interesting writing in witch it set
pydevd-pycharm server to develop.
Gimp is an interesting and used tool into linux community and not only. To have the support to python3 introduce a simplyest way to implement plugin based on AI like just do Gimp-ML with opencv, but with python3 instead of python2 and by adding some simple guide also the researcher can port their AI quickly to gimp to make theme ready in a short time.
My roadmap is to try to port some simple AI like Real-ESRGan for super resolution (DONE here but it need a GPU or a powerfull PC and a bit of patience, if you are only interesting in try it use a small image 32x32 pixels) and Generative Inpainting to redraw cancelled part automatically or port to python 3 and improve gimp-baloon-plugin that with some AI can be do automatically transcription, localization, translation and layer creations. I also track my work on this gimp forum thread.
Join the open source and Enjoy with us!