Customize CLUT in G'MIC

(Boris Hajdukovic) #1

I just discovered “Customize CLUT” filter in the GMIC and played with it a bit. I immediately noticed that is a very powerful tool for color manipulation of photos. The ability to quickly and accurately replace a color with another opens a wide range of possibilities to change photos stylistically.
What would make this filter even more powerful would be the possibility to replace a color with transparency - to be able to choose alpha channel as target color. Is this possible?

(Garry R. Osgood) #2

Straight forward, if you fancy getting into the underlying gmic code:

Figure out what color you wish to make transparent. Mouse around with the
dropper tool, perhaps. Here, I’ve settled on rgb: (60, 90, 200) and I’m playing
with this image.

Go to your gimp-gmic filter, select it, then choose under ‘Various’ the
Custom code option. Cut whatever is written in the Code textbox and replace
it with these lines:

    --fill_color. 60,90,200 # Set color: RGB
    -mul. '{2/(iM-im)}'
    -fill. 'gauss(i,0.7)'   # Set selectivity: Smaller more selective

Replace ‘60,90,200’ with your color. Set ‘Value Action’ to ‘Normalize’.
Set ‘Input/Output’ ‘Output Mode…’ to ‘New Layers’.

Your display should now look something like this.
The preview shows a mask that, when pasted into a selection channel,
will select your color most strongly, similar colors less
so. White indicates strongly selected pixels; black is deselected.
Experiment with the selectivity value, here 0.7, to vary the
drop-off in selectivity. Smaller numbers are more selective. Useful
range is about 0.0 -> 1.0. Change values as you see fit, click on the
"Update" button to apply changes to the preview.

When you have what seems to work best, hit “OK”. Your mask appears as
a new layer. Select it. Switch to channels, and click-drag any one of
the R, G or B channels down into the vacant area beneath “Lock.” That
becomes your selection mask for the color of interest. Right click on
it and choose “Channel to selection”. You should see marching ants all
over the place. Go back to layers; select your original image, add an
Alpha channel if it does not have one, then Edit->cut away the selected
area. Your image will vary in transparency depending how similar the
color is to your chosen color.

If you want an all-or-nothing binary kind of mask, then use:

  --select_color 30%,60,90,200

instead of the script above. Vary the percentage to set selectivity,
smaller percentage being more selective. Read about -select_color
and all of the other G’MIC commands used here at the G’MIC tutorial pages.


The script subtracts the color of interest from the image itself. That
makes the precise color of interest virtually a zero color vector; similar
colors will have near zero components. We then find the norms of all these
vectors, generating a field of scalars where the more dissimilar colors
are replaced by a grey that varies in proportion to dissimilarity. Running
a gaussian over the field inverts this sense: exact matches become value one,
less similar are darker. Adjusting the radius of the gaussian determines
the sharpness of the drop-off. See -select_color in the tutorials for more
in-depth theory.

Hope this helps.


Generating mid-tone masks

Thanks @grosgood for another great tutorial. I normally use -select_color but the method here is nuanced. One complaint is that your posts tend to hard wrap, lowering their readability on small screens.

(Boris Hajdukovic) #4

@grosgood thank you for your detailed tutorial!

Unfortunately I was not able to repeat your result because I use GIMP 2.9.5 and the RGB values ​​are different there :slight_smile: :


I know, I could “translate” it somehow, but that is the point where the problem starts for me - things are getting too complicated :slight_smile:

As far as G’MIC code is concerned, I know too that I could get a lot of things done much easier, but I’m to lazy, not talented and I’ve never had much patience required to deal with any code syntax :slight_smile:

What I like in the “Customize CLUT” filter is very simple and elegant way to selectively replace the colors: pick source color from preview window, choose target color from color picker, apply filter - done!
If you want to have it much more precise, you can also enter precise values ​​and can also change other variables but this does not have to be made if not necessary.

That being said, I will definitely look again at your tutorial because it offers nice possibility to vary the color selection and amount of transparency!

(Garry R. Osgood) #5

Thank you for pointing that out. How churlish of me. I’ve been composing my responses in emacs and then copy-pasting into the site editor. Here on out I’ll strip carriage returns except for paragraph breaks. Thank you again for pointing that out.
I appreciate your point of view and find no fault with it. Most people get a lot of work done solely through G’MIC-GIMP filters and generally do not even need to move on from them. That’s why I started with the premise “If you fancy…” knowing full well that – no, most people don’t fancy. For those who do fancy, though, and for those who might, someday, then the underlying script parser is there. Being primarily a tool-kit, one can try ideas out with it and, with experience, get to hit upon novel approaches – well before such get nicely packaged into filters. That is the pay-back for those who climb up the learning curve; you can be on the cutting edge of your own technical visions. A great deal of satisfaction arises from that. Plus, if you want to play with an idea, you don’t have to wait on anybody doing packaging for you. And Plus, Plus, if a filter doesn’t quite do what you would like it to do, you can take it apart and put it back together again in a way that precisely works for you – and, at the end of the day, have something to share with others in the gmic community.

Take care,


(David Tschumperlé) #6

Maybe more info about this: a CLUT is represented as a 3D RGB cube (i.e. a function from RGB to RGB).
In order to manage RGBA colors, we should create a 4D CLUT instead (a function from RGBA to RGBA).
This would be really cumbersome to interpolate keypoints in a RGBA space (would take at least 64x more memory, and I don’t speak about the increase of the computation time).
One work-around would be to manage a CLUT from RGB to RGBA instead, but then, it would be really equivalent to using a specific RGB color instead of alpha, then using the ‘Color to alpha’ filter (or as Garry suggested, use the ‘-replace_color’ command from G’MIC). And it would use more memory anyway, so I’m not really keen in doing this, as a cheaper alternative is possible.

(Boris Hajdukovic) #7

Thank you for clarifying things @David_Tschumperle ! :slight_smile:

“Customize CLUT” is great filter as it is and I can use nice tutorial from @grosgood for replacing color with transparency.