Page 1 of 1
Desaturate Red colors
Posted: 2016-09-26T10:04:47-07:00
by mkachar
I'm new to IM and need some help to write a simple IM command to desaturate red colors from a jpg image (but retaining all other colors). To do this in Photoshop I select Hue/Saturation then select REDS (from drop down menu) and move the saturation to -100. How can I do the same effect in ImageMagick.
Here is an example of the effect
https://app.box.com/s/by8xqffhl6mn4b03zqmnckjdcvwiqmeu
Re: Desaturate Red colors
Posted: 2016-09-26T10:27:42-07:00
by snibgo
I break down problems like this into:
1. What effect do I want? In this case, "-modulate 100,0,100".
2. Where do I want it? For example: "I want it where the red channel is at least 15% more than the blue channel." This is "-channel RB -separate +channel -compose MinusSrc -composite -threshold 15%".
We use the result of (2) as a mask to composite (1) over the input. So we get:
Code: Select all
convert "Desaturate Reds.jpg" ( -clone 0 -modulate 100,0,100 ) ( -clone 0 -channel RB -separate +channel -compose MinusSrc -composite -threshold 15% ) -compose Over -composite c.png
Re: Desaturate Red colors
Posted: 2016-09-26T11:05:35-07:00
by fmw42
If on Linux, MacOSX or Windows with Cygwin or Windows 10, you can try my script modulatecolor2 at the link below.
Re: Desaturate Red colors
Posted: 2016-09-28T09:29:25-07:00
by mkachar
snibgo wrote:I break down problems like this into:
1. What effect do I want? In this case, "-modulate 100,0,100".
2. Where do I want it? For example: "I want it where the red channel is at least 15% more than the blue channel." This is "-channel RB -separate +channel -compose MinusSrc -composite -threshold 15%".
We use the result of (2) as a mask to composite (1) over the input. So we get:
Code: Select all
convert "Desaturate Reds.jpg" ( -clone 0 -modulate 100,0,100 ) ( -clone 0 -channel RB -separate +channel -compose MinusSrc -composite -threshold 15% ) -compose Over -composite c.png
Thank You snibgo. This script works great except it desaturates yellow. When I do it in photoshop I'm able to keep yellows. Is there anyway to modify the code in order to keep yellow ?
Thank you
Re: Desaturate Red colors
Posted: 2016-09-28T11:07:29-07:00
by snibgo
You need to define exactly what you mean by "desaturate red colors ... but retaining all other colors". What defines "red" as opposed to "not red"? And is the boundary between these sharp (so it is binary, and any pixel is either red or not-red) or is there a transition?
You can play with the expression I gave in (2). For example, you could use "-channel R -separate +channel". This would be a smooth transition between red and non-red.
Try Fred's script. Does it do what you want? If not, what do you want different?
Re: Desaturate Red colors
Posted: 2016-09-29T11:48:34-07:00
by snibgo
Here is a v7 command to reduce saturation of a particular hue, or range of hues. Windows BAT syntax.
Code: Select all
set centHue=0
set rangeHue=10
set transHue=10
%IMG7%magick ^
rose: ^
( -clone 0 -modulate 100,0,100 ) ^
( -clone 0 ^
-colorspace HSL ^
-channel H -separate +channel ^
-evaluate AddModulus %%[fx:50-%centHue%]%% ^
-solarize 50%% ^
-evaluate Multiply 2 ^
-level %%[fx:100-%rangeHue%-2*%transHue%],%%[fx:100-%rangeHue%]%% ^
) ^
-compose Over -composite ^
rose_desat.png
The controls are all percentages:
centHue determine which hue will be desaturated. 0=red, 16.67=yellow, 33=green, 67=blue etc. 100 is also red.
rangeHue determines what range of hues will be entirely desaturated. "10" means "plus or minus 5%".
transHue is the percentage of hues over which a transition will be applied. In this example, hues greater than 0 + 10/2 + 10 = 15% will not be desaturated. As yellow is more than 15%, it will not be desaturated.
This works for all hues, wrapping as required for 0=100.