Posts: 13
Threads: 5
Joined: May 2020
Reputation:
0
In GIMP 2 you could threshold the alpha channel (Layer > Transparency > Threshold Alpha...), in Python console equivalent was:
Code:
image = gimp.image_list()[0]
layer = image.layers[0]
# image,layer,threshold.
pdb.plug_in_threshold_alpha(image,layer,0)
In GIMP 3 Threshold Alpha has become a filter ("fx" icon next to the layer)
I can do in GIMP 3:
Code:
image = Gimp.get_images()[0]
layer = image.get_selected_layers()[0]
# Add Threshold Alpha layer fx (Layer > Transparency > Threshold Alpha...).
filter1 = Gimp.DrawableFilter.new(layer, 'gimp:threshold-alpha', 'My Threshold Alpha')
# Add Gaussian Blur layer fx (Filters > Blur > Gaussian Blur...).
filter2 = Gimp.DrawableFilter.new(layer, 'gegl:gaussian-blur', 'My Gaussian Blur')
layer.append_filter(filter1)
layer.append_filter(filter2)
But this does the filters in default settings.
How do I change the settings of the filters, for example how do i set the alpha value to 0 of 'My Threshold Alpha'?
Posts: 13
Threads: 5
Joined: May 2020
Reputation:
0
04-02-2025, 11:34 PM
(This post was last modified: 04-03-2025, 12:01 AM by joeyeroq.)
I couldn't find any usefull documentation on DrawableFilterConfig methods. I googled some more found this thread on gimpchat.com GIMP 3 now has GEGL automation in python console. So to set parameters for filters you do this:
Code:
# Get image and selected layer.
image = Gimp.get_images()[0]
layer = image.get_selected_layers()[0]
# Add Threshold Alpha layer fx (Layer > Transparency > Threshold Alpha...).
filter_threshold_alpha = Gimp.DrawableFilter.new(layer, 'gimp:threshold-alpha', 'My Threshold Alpha')
To print the parameters in the console you can use DrawableFilter.get_config().list_properties():
Code:
for parameter in filter_threshold_alpha.get_config().list_properties():
parameter.get_name(), parameter.get_default_value()
# To set the parameter to 0 you do
filter_threshold_alpha.get_config().set_property('value', 0.0)
# Append Threshold Alpha filter to the layer.
layer.append_filter(filter_threshold_alpha)
Now let's add Gaussian Blur
Code:
# Add Gaussian Blur layer fx (Filters > Blur > Gaussian Blur...).
filter_gaussian_blur = Gimp.DrawableFilter.new(layer, 'gegl:gaussian-blur', 'My Gaussian Blur')
# set properties for Gaussian Blur.
filter_gaussian_blur.get_config().set_property('std-dev-x', 5.5)
filter_gaussian_blur.get_config().set_property('std-dev-y', 5.5)
# If you set the properties after adding the filter to the layer you have to use:
filter_gaussian_blur.update()
I tried to add all properties for Gaussian Blur at once but
Code:
filter_gaussian_blur.get_config().set_properties(('std-dev-x', 1.5), ('std-dev-y', 1.5), ('filter', 'auto'), ('abyss-policy', 'clamp'), ('clip-extent', True))
and
Code:
filter_gaussian_blur.get_config().set_properties(5.5, 5.5, 'auto', 'clamp', True)
didn't work either. To merge the filters down use:
Code:
layer.merge_filters()
How do you find out what the filter operation’s name are like 'gimp:threshold-alpha' and 'gegl:gaussian-blur' when using Gimp.DrawableFilter.new(drawable, operation_name, name)?
Posts: 6,651
Threads: 289
Joined: Oct 2016
Reputation:
586
Gimp version:
Operating system(s): Linux
Some pointers (no time to develop more that this ATM):
- https://gegl.org/operations/ (there may be some more that are gimp-specific).
- https://gitlab.gnome.org/GNOME/gimp/-/merge_requests/2008
- https://developer.gimp.org/api/3.0/libgimp/method.Drawable.merge_new_filter.html
Posts: 13
Threads: 5
Joined: May 2020
Reputation:
0
Thanks to HoneyBadger at GIMP Discord:
HoneyBadger wrote 2025-04-05T06:28:14.224Z about getting the name of operations like 'gimp:threshold-alpha' and 'gegl:gaussian-blur':
You can extract the names of operations from the filters applied to the layer:
Code:
Gimp.get_images()[0].get_layers()[0].get_filters()[0].get_operation_name()
HoneyBadger wrote 2025-04-04T21:04:43.798Z about adding all properties of a filter at once in stead of one by one:
It turns out the documentation is not complete, the method really exists
You can use it like this:
Code:
config.set_properties(prop1=value1, prop2=value2)
But if the property name contains a hyphen, you will have to do it differently:
Code:
my_props = {
'std-dev-y': 3,
'std-dev-x': 3
}
config.set_properties(**my_props)
Or:
Code:
config.set_properties(**{ 'std-dev-y': 3, 'std-dev-x': 3 })
You can even mix them together if you want:
Code:
config.set_properties(filter='auto', **{ 'std-dev-y': 3, 'std-dev-x': 3 })
Posts: 6,651
Threads: 289
Joined: Oct 2016
Reputation:
586
Gimp version:
Operating system(s): Linux
Yesterday, 05:31 PM
(This post was last modified: Yesterday, 05:31 PM by Ofnuts.)
In practice for a similar call I use a wrapper:
Code:
def callProcedure(procId: str, run_mode=Gimp.RunMode.NONINTERACTIVE, **kwargs):
procedure=Gimp.get_pdb().lookup_procedure(procId)
if procedure is None:
raise Exception(f'Procedure "{procId}" no found')
config: Gimp.ProcedureConfig = procedure.create_config()
#config.set_property('run-mode',run_mode)
for name,value in kwargs.items():
if isinstance(value,list):
config.set_core_object_array(name.replace('_','-'),value)
else:
config.set_property(name.replace('_','-'),value)
result=procedure.run(config)
results = [result.index(i) for i in range(result.length())]
if results[0] is not Gimp.PDBStatusType.SUCCESS:
raise Exception(f'Error returned from procedure "{procId}": {results[0].value_name}')
return results[1:]
So you can call the procedure with Python keyword arguments:
Code:
res = callProcedure('plug-in-sel2path', image=image,
drawables=[selectableLayers[0]],
align_threshold=.05,
corner_surround=1,
error_threshold=.05,
filter_epsilon=1,
filter_iteration_count=1,
filter_percent=0.
)
And the callProcedure code converts python-friendly argument names such as filter_epsilon into Gimp friendly names such as filter-epsilon.
|