Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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"]
share|improve this question
2  
I think ColorSpace works only with Import, not with Export. Perhaps, you can ColorConvert[something,"Grayscale"] and then export as a workaround? –  kguler Nov 20 '12 at 15:40
    
Related: mathematica.stackexchange.com/q/8818/121 –  Mr.Wizard Nov 21 '12 at 0:19
add comment

2 Answers

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.

share|improve this answer
    
@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. 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 3:50
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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