Function Repository Resource:

FaustGrayscaleConvert

Source Notebook

Convert a color or an image to grayscale using Faust's method

Contributed by: Jan Mangaldan

ResourceFunction["FaustGrayscaleConvert"][image]

converts the color space of image to grayscale.

ResourceFunction["FaustGrayscaleConvert"][color]

converts color specifications of a color directive.

Details and Options

ResourceFunction["FaustGrayscaleConvert"] can be used to change the color specification of an image to grayscale.
ResourceFunction["FaustGrayscaleConvert"] works with colors and arbitrary 2D and 3D images.
ResourceFunction["FaustGrayscaleConvert"] takes a Method option, which specifies the initial color space conversion done before applying Faust's conversion method. Possible settings for the Method option are "LCH" and "HSB"; these are described e.g. in the documentation for ColorConvert.

Examples

Basic Examples (2) 

Convert a color photo to grayscale:

In[1]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/36087dbc-7254-4831-8ca7-4c017528de3a"]
Out[1]=

This shows that the previous output is a grayscale image:

In[2]:=
ImageColorSpace[%]
Out[2]=

Convert an RGB color object to grayscale:

In[3]:=
ResourceFunction["FaustGrayscaleConvert"][RGBColor[1, 0, 0]]
Out[3]=
In[4]:=
% // InputForm
Out[4]=

Scope (4) 

Convert any color in any color space to grayscale:

In[5]:=
ResourceFunction["FaustGrayscaleConvert"][
  XYZColor[1, 1, 0]] // InputForm
Out[5]=

Convert a list of colors:

In[6]:=
ResourceFunction[
  "FaustGrayscaleConvert"][{Red, Green, Blue}] // InputForm
Out[6]=

Convert a list of images:

In[7]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/10a737f1-0ca8-4a9f-8c4f-06fe25cb6508"]
Out[7]=

Convert a 3D image:

In[8]:=
ResourceFunction["FaustGrayscaleConvert"][\!\(\*
Graphics3DBox[
TagBox[Raster3DBox[CompressedData["
1:eJztlTFOA0EMRS2goIMrcAB6WkraIA6QiCWiCVKChDg9bIG2ye7M2PM98iz/
iSCB1qOXb4/3bvexebsQkdP1+Guz/Xo8Hrffz7fjHy+H0/v+MLw+HT6H/XB8
2F2N/7z5+9yPn0shhBBCCCGEEEIIWQs/uR9q9KQBFTVqoCNrqFEUbZdpCNKh
SgMKNQgpIciI9q0RYZk7pNZQo+gF5ayha/La08gr9npTHAiiQcgSQUa0470R
YYva+wd9pxg1dE320TD2eaVpTJV5S8XZFoe8olrjvKwaowYhrbCPKHSIe90b
QbaoUUO3DjNnu+1cf43ajOOmkS5IAZ0Nu4akn3Drtg5jtwUmQEgat4Xkr2G8
sNC9EWGLurXQX6M2Y4SGz8G9pjFbVuqKiHmprEgUF/NsWd4SHTMhGBqOKPqm
WC4sem9Y1hd6i7pl56zhc7Db0/8gjUSNQrfu+yVqSl0R32+ppki09Hidw1RD
iJqGI4rWsFxYUW0Wn/Ul2Yd0Gm4t9NeozRih4XNwr2nMlpW6ImJeKisSxcU8
W5a3RMdMCAbjiAp4PhteWKiGcX0JeG9E2KJuy85fozbjuGmkC1JAZ8OuIekn
3Lqtw9htgQkQksY+ohE00K697o0gW9SooWuyj4axzytNY6rMWyrOtjjkFdUa
52XVGDUIaUWQEe1bI8LeCLJFjRoR3im6Jq89jbxirzfFgSAahCwRZESpgdGA
iva9zC0aRdF2mYYgHao0oFCDEEIIIYQQQgghZB38AiibATU=
"], {{0, 15, 15}, {
      45, 0, 0}}, {0, 255},
ColorFunction->"RGBDefaultColorFunction",
Method->{"FastRendering" -> True}],
BoxForm`ImageTag["Byte", ColorSpace -> "RGB", Interleaving -> True],
Selectable->False],
AxesStyle->{},
Background->None,
BaseStyle->"Image3DGraphics3D",
BoxRatios->Automatic,
Boxed->False,
ImageSizeRaw->45,
PlotRange->{{0, 45}, {0, 15}, {0, 15}}]\)]
Out[8]=

Options (1) 

Method (1) 

Use Method to specify the initial color conversion performed:

In[9]:=
Table[ResourceFunction["FaustGrayscaleConvert"][
  ExampleData[{"TestImage", "JellyBeans"}], Method -> met], {met, {"LCH", "HSB"}}]
Out[9]=

Applications (1) 

Use FaustGrayscaleConvert to convert all colors in a plot to grayscale:

In[10]:=
MoleculePlot3D[Entity["Chemical", "Deltamethrin"], PlotLegends -> Automatic] /. c_?ColorQ :> ResourceFunction["FaustGrayscaleConvert"][c]
Out[10]=

Properties and Relations (1) 

FaustGrayscaleConvert often gives results with a better intensity distribution than ColorConvert[input,"Grayscale"], but takes more time to evaluate:

In[11]:=
AbsoluteTiming[
 ResourceFunction["FaustGrayscaleConvert"][
  ExampleData[{"TestImage", "Apples"}]]]
Out[11]=
In[12]:=
AbsoluteTiming[
 ColorConvert[ExampleData[{"TestImage", "Apples"}], "Grayscale"]]
Out[12]=

Version History

  • 1.0.0 – 17 May 2021

Source Metadata

Related Resources

License Information