It is currently Tue Jul 21, 2026 8:51 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: issue with 'select_color' in python script
PostPosted: Thu Nov 20, 2025 4:51 am  (#1) 
Offline
New Member

Joined: Oct 15, 2021
Posts: 4
Hi, i am having an issue with a script i am working on. below is an MWE to illustrate the issue (apparently one cannot attach .py, .txt, ... files here?).

the problem arises when calling this (or with "#00000000")
image.select_color(Gimp.ChannelOps.REPLACE, layer, Gegl.Color.new("transparent"))


it selects all transparent pixels except black. so it seems as if it is considering all other colors to be transparent, as demonstrated here:
Image

the context settings can be seen in the image on the left. what am i doing wrong? is this a bug?

i am using the following:
gimp 3.0.6 flatpak
fedora 43
intel chipset
32gb ram

thank you all in advance!

#!/usr/bin/env python
# -*- coding: utf-8 -*-

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, GLib, Gtk, Gio, Gegl
import sys, os

def issue_demo(procedure, run_mode, image, drawables, config, run_data):
    image.undo_group_start()

    for layer in image.get_selected_layers():
        image.select_color(Gimp.ChannelOps.REPLACE, layer, Gegl.Color.new("transparent"))

        return

    image.undo_group_end()
   
    Gimp.displays_flush()
    return



class IssueDemoPlugIn(Gimp.PlugIn):

    def do_query_procedures(self):
        return ["issue-demo"]

    def do_create_procedure(self, name):
        if name != "issue-demo":
            return None

        procedure = Gimp.ImageProcedure.new(self,
                                            name,
                                            Gimp.PDBProcType.PLUGIN,
                                            issue_demo,
                                            None)

        procedure.set_image_types("RGB*")
        procedure.set_sensitivity_mask(Gimp.ProcedureSensitivityMask.ALWAYS)
        procedure.set_menu_label("issue demo")
        procedure.set_attribution("Ingegneus", "Ingegneus", "2025")
        procedure.add_menu_path("<Image>/Filters")
        procedure.set_documentation(
            "issue demo",
            "issue demo",
            None)

        return procedure

   
# Plugin entry point
Gimp.main(IssueDemoPlugIn.__gtype__, sys.argv)


Attachments:
issue-demo.xcf [778.47 KiB]
Downloaded 63 times
Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: issue with 'select_color' in python script
PostPosted: Thu Nov 20, 2025 10:19 am  (#2) 
Offline
GimpChat Member
User avatar

Joined: Jul 04, 2019
Posts: 282
Location: Lake Havasu City, Arizona, USA
Hi,

According to the documentation, the context settings effect the processing.

Try setting the context using the context-setter-functions found here.

I would suggest turning merged-sampling off and setting the sample-threshold to a low value to get a limited the selection.

_________________
Charles


Top
 Post subject: Re: issue with 'select_color' in python script
PostPosted: Thu Nov 20, 2025 11:35 am  (#3) 
Offline
New Member

Joined: Oct 15, 2021
Posts: 4
thanks for the quick reply.
okay... changing the sample-threshold does fix it, but it still doesnt make any sense to me...

1) i thought the 'context' settings were the ones you could change in the UI. Like the foreground/background color, line width, etc. ... so it wouldnt make any sense to set them explicitly if they are fine as is.
2) apparently Gimp.context_set_sample_threshold is a value from 0 -> 1. it might be good to add that in the docs
3) the default value for the threshold is 0.058823529411764705. where does this value come from? i tried changing the threshold values in the UI, but nothing changed to the return value of Gimp.context_get_sample_threshold
4) what does this value of 0 -> 1 represent? if i set it to 0 then the selection is just a tiny black sliver. if i set it to 1 it selects what i wanted in my OP.


Top
 Post subject: Re: issue with 'select_color' in python script
PostPosted: Fri Nov 21, 2025 10:23 am  (#4) 
Offline
GimpChat Member
User avatar

Joined: Jul 04, 2019
Posts: 282
Location: Lake Havasu City, Arizona, USA
I find the context settings add a lot complexity to my code, but, as far dependencies, they are well documented in the PDB. Without the context settings, a plug-in or script would be left with very little control over the finer details of Gimp production.

Whenever I see an option or variable that goes 0 -> 1, I typically translate it as a high precision floating point range. What isn't clear in the sample-threshold setting is what the difference is between the low and high values. In my experiments, I've found that the higher the value, the greater the selection.

I believe sample-threshold's default value works well during sampling when there is antialias pixel separating transparent and opaque pixel.

I don't know exactly how the interface affects the context settings. I've tested it as you have, and found the same result.

Also, I've discovered when the sample-transparency context setting is a True value, transparency is selected.

_________________
Charles


Top
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group