4
$\begingroup$

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"]
$\endgroup$
2
  • 2
    $\begingroup$ I think ColorSpace works only with Import, not with Export. Perhaps, you can ColorConvert[something,"Grayscale"] and then export as a workaround? $\endgroup$
    – kglr
    Nov 20, 2012 at 15:40
  • 1
    $\begingroup$ Related: mathematica.stackexchange.com/q/8818/121 $\endgroup$
    – Mr.Wizard
    Nov 21, 2012 at 0:19

2 Answers 2

7
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ @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. $\endgroup$
    – geordie
    Nov 20, 2012 at 23:48
  • 2
    $\begingroup$ Alternatively: toGrayScale[y_] := y /. x : (_RGBColor | _Hue | _CMYKColor) :> ColorConvert[x, GrayLevel] $\endgroup$ Nov 20, 2012 at 23:48
  • 1
    $\begingroup$ @Grrwebb No worries, you don't have to delete this at all; I'm glad that you found it useful :) $\endgroup$
    – rm -rf
    Nov 21, 2012 at 3:50
5
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.