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
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,
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.
10-05-2025, 07:51 AM (This post was last modified: 10-05-2025, 07:55 AM by rich2005.
Edit Reason: typo
)
(10-05-2025, 01:02 AM)elindarie Wrote: I don't know if this will help any other beginners on Gimp 3 Python scripting.
....snip...
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.
I am in the same boat, looked at all the available advice, looked at numerous python plugins, left as totally baffled. I hate to say it but script-fu is a little (not much) easier.
It will come with time, there will be documentation that is above "hello world" in the python console but more readable than the present situation.
Anyway, as a dyed-in-the-wool dabbler and for my linear-code requirements I use a simple-ish shell. Your code applies like this, I cannot be bothered to change the registration The entry point where it goes is after #Create Filter (but filters are a bit of a mystery as well) FWIW plugin attached.
It's now 1/2026, and there is still a serious lack of help and tutorials for gimp 3 plugins. I guess this is partly because of the transition and major changes from gimp 2 to gimp 3. There's not much knowledge about - though please prove me wrong, and those who have it are probably busy on other things.
I'm pretty new to gimp - used it for tweaking a few images in the past and that's it, and whilst I have a lot of s/w design experience, absolutely none of it was python which I looked at for the first time last week. I did, a long time ago, write some VB automation stuff for driving Photoshop, but the similarity is zero.
There is documentation in the form of auto generated stuff, but, whilst that is useful in its place, it just describes the bits, not how the bits hang together as a system. For a beginner on this like me, that overall picture of architecture and structure is what I don't have.
I don't think it's reasonable to expect gimp developers to down tools and write loads of documents - they won't, anyway - so as far as I can see the only way to shed more light is for people like us to publish bits of code here and there, asking questions and for comments. Gradually knowledge may increase.
Cannot contribute much myself at the moment - I haven't got anything to work so far. I do have a shell plugin that registers, runs and pops up a dialog, but that's it - doesn't do anything else yet. It's supposed to load images one by one, make some changes, and write them out elsewhere as JPEGs - if only I can figure out how to open an image file, display it and write it out and discard it again. I can add that if it helps.
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,
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.
You're looking at the documentation for C, so it's normal that it's not obvious.
One has still to figure out that Gimp.ChannelOps(value) can be written Gimp.ChannelOps.REPLACE, which I recognize is not obvious. Took me a few tries.
I guess (didn't try) the short form would be Gimp.ChannelOps(2), but it's less explicit.
If you want to go one step further, I'd suggest you, as an exercise, to try to select the current image instead of creating one. ;-)
In the interests of passing knowledge around on writing plugins, particularly for Gimp 3, below is one (the first one) that I've just created. It's pretty simple - is given a source and dest directory, parses the src, and for each image creates a scaled down image and a thumbnail suitable for web display. I used to do this with VB automation and Photoshop, and it is specifically for the way I save photographs in a database. However, I found getting anyuthin g to work from scratch damn hard work, so if this helps anyone on the basics of writing a plugin, so much the better. Equally if anyone wants to comment, fine.
It has two problems at the moment:
1. paths returned from the plugin dialog are GFiles, and I could find no good way of turning them into a string path that I can manipulate. In the end I wrote a bodgey gfile_to_fullpath() that strips off the scheme for Windows and Linux.
2. the images are displayed as they are scaled, but not correctly. The final output files are fine, however. This didn't happen when testing it line by line in the python console - ie slowly, so I suspect it is something like the display and image manipulation are asynchronous, and the display doesn't keep up. But, don't really know.
Anyway, here it is for anyone interested, it seems to work in Ubuntu 24.04 and Windows 11, both on GIMP 3.0.6:
import csv
import math
import sys
import os
import string
import gi
gi.require_version('Gimp', '3.0')
from gi.repository import Gimp
gi.require_version('GimpUi', '3.0')
from gi.repository import GimpUi
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Gio
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
DESCRIPTION = """This reads images from a source directory and
writes them out again to a destination directory.
For each source image, a destination image is created 'nn.jpg' which is
scaled down if necessary, maintaining aspect ratio, to have a width or
height no more than a maximum set side, then a second (thumbnail) is
created named t_nn.jpg scaled if necessary to be no larger than a maximum
thumbnail side. nn is a 1 based index incrememted as it reads the images.
The destination folder is wiped of .jpg images before it starts."""
def purge_dest_folder(dest_folder):
files_purged = 0
with os.scandir(dest_folder) as iter:
for entry in iter:
if entry.is_file():
entry_extn = os.path.splitext(entry.name)[1].lower();
if entry_extn.lower() == ".jpg":
files_purged += 1
os.remove(entry.path)
return files_purged
###################################
def check_extension(inpath):
for e in IMAGE_EXTENSIONS:
if inpath.lower().endswith(e):
return True
infile = Gio.File.new_for_path(input_filepath) # creates a GFile that can be used in load
img = Gimp.file_load(Gimp.RunMode.NONINTERACTIVE, infile)
disp = Gimp.Display.new(img)
img.flatten()
### do any rotation here
# prepare save procedure
procedure = Gimp.get_pdb().lookup_procedure('file-jpeg-export')
config = procedure.create_config()
config.set_property('run-mode', Gimp.RunMode.NONINTERACTIVE)
config.set_property('image', img)
config.set_property('options', None)
# rest of parameters left default, except maybe jpeg quality, see below
output_filepath = os.path.join(output_folder, "t_" + str(file_index) + ".jpg")
outfile = Gio.File.new_for_path(output_filepath)
config.set_property('file', outfile)
config.set_property('quality', JPEG_LEVELS["max-q"]) # set jpeg quality to max for thumbnail
result = procedure.run(config)
# tidy up and delete
img.clean_all()
disp.delete()
###################################
def parse_images(src_folder, dest_folder, max_allowed_image_side_px, max_allowed_thumbnail_side_px, jpeg_level):
file_index = 0
with os.scandir(src_folder) as iter:
for entry in iter:
if entry.is_file():
entry_extn = os.path.splitext(entry.name)[1].lower();
for extn in IMAGE_EXTENSIONS:
if extn == entry_extn:
file_index += 1
convert_to_jpeg(entry.path, dest_folder, file_index, max_allowed_image_side_px, max_allowed_thumbnail_side_px, jpeg_level)
break
return file_index
###################################
def gfile_to_fullpath(gf):
uri = gf.get_uri()
full_path = uri
length_uri = len(uri)
file_scheme = "file:"
length_file_scheme = len(file_scheme)
index_scheme_start = uri.lower().find(file_scheme)
if index_scheme_start == 0: # found scheme at start of uri
index = length_file_scheme
if length_uri > length_file_scheme: # more characters
if uri[index] == "\\": # windows style
while index < length_uri and uri[index] == "\\": # skip
index += 1
full_path = uri[index:] # rest of string
elif uri[index] == "/": # unix style
while index < length_uri and uri[index] == "/": # skip
index += 1
index -= 1 # need the last '/'
full_path = uri[index:] # rest of string
else: # goodness knows
pass
else: # nothing after the scheme, goodness knows
pass
elif index_scheme_start > 0: # found scheme but not at start, goodness knows
pass
else: # didn't find scheme
pass
return full_path
###################################
###################################
#### run procedure that gets called to run the plugin