Function Repository Resource:

DesaturateExcept

Source Notebook

Desaturate an image except for a specified color or colors

Contributed by: Jeff Bryant

ResourceFunction["DesaturateExcept"][image,color]

desaturate the specified image except for the specified color.

ResourceFunction["DesaturateExcept"][image,color,d]

desaturate the specified image except for colors with maximum distance d around color.

ResourceFunction["DesaturateExcept"][image,{color1,color2,}]

desaturate the specified image except for the specified colors.

ResourceFunction["DesaturateExcept"][image,{color1,color2,},d]

desaturate the specified image except for colors with maximum distance d around the specified colors.

Details and Options

The default value for d is .21.
By default, the non‐specified colors are completely desaturated.

Examples

Basic Examples (4) 

Desaturate the image except for colors near Red:

In[1]:=
With[{image = Import["https://farm6.staticflickr.com/5572/15026495727_f1fa9f541b_o_d.jpg"]},
 GraphicsRow[{image, ResourceFunction["DesaturateExcept"][image, Red]}, ImageSize -> 600]]
Out[1]=

The distance from the specified color can be constrained to include smaller or larger regions:

In[2]:=
With[{image = Import["https://farm6.staticflickr.com/5572/15026495727_f1fa9f541b_o_d.jpg"]},
 GraphicsRow[{image, ResourceFunction["DesaturateExcept"][image, Red, .01]}, ImageSize -> 600]]
Out[2]=

Desaturate the image except for colors near a specified RGBColor:

In[3]:=
With[{image = Import["https://farm2.staticflickr.com/1721/42726258191_0fbb0fb450_o_d.jpg"]},
 GraphicsRow[{image, ResourceFunction["DesaturateExcept"][image, RGBColor[
    0.2733974426246861, 0.5515053024911598, 0.9292455896326007]]}, ImageSize -> 600]]
Out[3]=

Desaturate the image except for colors near Yellow and Red:

In[4]:=
With[{image = Import["https://farm1.staticflickr.com/907/28003185018_6047d40855_o_d.jpg"]},
 GraphicsRow[{image, ResourceFunction["DesaturateExcept"][image, {Yellow, Red}]}, ImageSize -> 600]]
Out[4]=

Options (1) 

"DesaturationFraction" can be used to adjust how much desaturation to use:

In[5]:=
With[{image = Import["https://farm6.staticflickr.com/5572/15026495727_f1fa9f541b_o_d.jpg"]},
 GraphicsRow[{image, ResourceFunction["DesaturateExcept"][image, Red, "DesaturationFraction" -> .75]}, ImageSize -> 600]]
Out[5]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 20 March 2019

License Information