Desaturate Red colors

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
mkachar
Posts: 2
Joined: 2016-09-26T09:30:03-07:00
Authentication code: 1151

Desaturate Red colors

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Desaturate Red colors

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Desaturate Red colors

Post by fmw42 »

If on Linux, MacOSX or Windows with Cygwin or Windows 10, you can try my script modulatecolor2 at the link below.
mkachar
Posts: 2
Joined: 2016-09-26T09:30:03-07:00
Authentication code: 1151

Re: Desaturate Red colors

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Desaturate Red colors

Post 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?
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Desaturate Red colors

Post 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
Image
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.
snibgo's IM pages: im.snibgo.com
Post Reply