4

The help for the Export function is absolutely unclear. How can I specify options for my output format?

According to the documentation, PNG supports grayscale output. But this doesn't work, I still get an RGB image.

Export["somefile.png", something, "ColorSpace" -> "Grayscale"]
Lorenzo Pistone
  • 1,607
  • 11
  • 16
  • 2
    I think ColorSpace works only with Import, not with Export. Perhaps, you can ColorConvert[something,"Grayscale"] and then export as a workaround? – kglr Nov 20 '12 at 15:40
  • 1
    Related: http://mathematica.stackexchange.com/q/8818/121 – Mr.Wizard Nov 21 '12 at 00:19

2 Answers2

7

The following solution works as a formatting step prior to performing the export of your output

(Please note that this is not my solution! it was posted here by @rm -rf - i'm reposting it because it got a little lost in the comments/editing of the other question and it seems to be a very effective post-processing method).

Where other approaches do not work, this one seems to get over the line.

toGrayScale[y_] := y /. x__?
(MemberQ[{RGBColor, Hue, CMYKColor}, Head[#]] &) :> ColorConvert[x, "Grayscale"]

greyscalemyPlot = myPlot // toGrayScale

Export["greyscale-myplot.eps", greyscalemyPlot]

I found this particularly useful when I have an array of plots like a GraphicsGrid of ListPlots or where I have used Show to overlay two plots. in both cases it is a pain to go back in and modify the options using GreyLevel etc.

geordie
  • 3,693
  • 1
  • 26
  • 33
  • @rm -rf, yes, that is what I meant, thanks. Happy to remove this answer if you want to post similar or simply redirect back to the original answer. As I said I have found it to be a very handy post-processing approach. Cheers. – geordie Nov 20 '12 at 23:48
  • 2
    Alternatively: toGrayScale[y_] := y /. x : (_RGBColor | _Hue | _CMYKColor) :> ColorConvert[x, GrayLevel] – J. M.'s missing motivation Nov 20 '12 at 23:48
  • 1
    @Grrwebb No worries, you don't have to delete this at all; I'm glad that you found it useful :) – rm -rf Nov 21 '12 at 03:50
5

The documentation can be a bit confusing sometimes, but in this case it is clearly stated in the elements section of the png help-page

enter image description here

The option "ColorSpace" is only for Import.

Anyway, what happens when you Export a png-file is, that (when it is a Graphics or Graphics3D) Mathematica calls Rasterize which accepts the ColorSpace option. Therefore one solution to your problem is to use

Export["tmp/gray.png", Rasterize[p, ColorSpace -> "Grayscale"]]

A quick test shows, that it creates identical images which only differ in ColorSpace

p = Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -2, 2}];
Export["tmp/color.png", p]
Export["tmp/gray.png", Rasterize[p, ColorSpace -> "Grayscale"]]

Function[{img}, 
   Import[img, #] & /@ {"BitDepth", "ColorSpace", "DataType", 
     "ImageSize"}] /@ {col, gray} // Column
(*
 {8,RGBColor,Integer,{360,281}}
 {8,GrayLevel,Integer,{360,281}}
*)

and they fit exactly onto another

ImageMultiply @@ (Import /@ {col, gray})

Mathematica graphics

halirutan
  • 112,764
  • 7
  • 263
  • 474