PythonFu plugin: Analyse & Select by Color Range
The purpose of this tool is to make it easier to select regions from an image based on a well defined color range, for instance selecting a blue and white sky background.
The tool has two sections, the Analyzer and The Color Range Selector. The Analyzer will read through each pixel of the current selection and find the maximum and minimum red, green and blue values, which defines a color range, and also the associated mid-values and thresholds which also define the range.
After running an analysis, with a button click those values can be loaded into the Color Range Selector. By then running the selector on that range you expand from the original sample selection to include all pixels within that color range. If some of the region you want is still not included within the selection, you can use the rectangle, ellipse or other selection tool to ADD a sample of the excluded region to the current selection. Then re-run the analysis, load the new range into the selector and expand the selection again to also include all pixels which match those from the second sample. You can repeat this process and creep up to the point where hopefully you have found just the right color range which includes your target region, but no more (works best with well-behaved images :)
In order to allow continuous interaction with the user like this, the plugin doesn't rely on the usual Python-Fu dialog for a one shot input of parameter values, but rather explicitly builds it's own dialog through the pygtk module. Pygtk allows us to call the Gimp Tool Kit routines so our dialog persists and we can get new input values as we go.
The Color Range Selector differs from the Gimp's Color Selector in that it lets you select based on composite RGB but allows different thresholds for each of the RGB channels. It also allows for .5 values of mid points and thresholds. This way you can select for the exact range which matches a sample selection, no more and no less.
Color Range Selector manages this by making multiple calls to pdb.gimp_image_select_color() and building the INTERSECTION of separate selections from the red, green, and blue channels.
Both the Analyzer and the Selector will work on RGB or Gray layers and even channels. While working you can switch to different layers or channels but not to a different image (I haven't yet figured out a way for the plugin to know that the user has switched to a different image.) You can run an analysis on one layer and switch to another to select.
Works on RGB* and GRAY* images, but not Indexed, maybe in the next
To install, copy select_color_range.py to your personal gimp plugins directory, in Windows this would be something like:
'Analyse & Select Color Range' should appear in Color Menu after Gimp starts up.
An annoyance: be aware if you undo a color range selection, Gimp may may end up making a channel active instead of leaving your current layer active. Before proceeding you'll have to click on the correct layer. This happens because sometimes Color Range Select has to save intermediate selections as a channel, which it later deletes, which results in the active being in the channel list. The plugin then explicitly resets the initial drawable as the active. For whatever reason the undo leaves us with an active channel in those cases and I don't know how to fix this at this point.
March 6, 2014 - 15:04 — Doctor.Mu
I've updated the file select_color_range.py so that it no longer computes the histograms of RGB vals and unique colors. That feature is not used for the selector part of the tool and eliminating it speeds up the analysis. In the future the histogram results will be included in a color change tool I'm currently working on.