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.

Is there a way to Export expressions to a monochrome PNG?

I have tried the ColorQuantize function which is mentioned in comments to this question. However, the resulting PNG image of ColorQuantize[TraditionalForm[MyExpression], 2] is still of depth 24b. GIF file's depth appears to be 2, which is OK but it still looks like a cheat, and yes, PNG is more desired in my case. Rasterizing formulas before exporting also seems to be an unnatural hack that could and should be avoided in some way.

I could Export my expressions to PDF and then GhostScript them (say, via Run) using GhostScript's pngmono device but Mathematica's current PDF Export is quite buggy. (It doesn't even print \[Bullet] correctly; TraditionalForm is lost, etc.) Printing selection to PDF and GhostScripting the result would do the thing for me… but there seems to be no way to do it ‘in a background mode’ using Mathematica commands.

share|improve this question
    
Do you mean ColorConvert[expr, "Grayscale"]? –  rm -rf Jul 28 '12 at 23:46
    
I think he wants just black and white only not grayscale. What about Binarize? –  s0rce Jul 28 '12 at 23:49
    
@s0rce Yes, "monochrome" means "black and white" here. –  Akater Jul 28 '12 at 23:57
    
@s0rce Exporting binarized image results in 48b-depth png of awful quality. –  Akater Jul 29 '12 at 0:06

2 Answers 2

up vote 3 down vote accepted

In version 8 the standard form of Export seems to always produce PNG files with a bit depth of 16 and RGB colorspace. The desired 1 bit output can be achieved by using the "Rules" syntax as follows:

expr = HoldForm@Sum[Sin[i Sqrt[2]], {i, 0, 10}];

Rasterize[TraditionalForm@expr, ImageSize->200, RasterSize->600] ~ Binarize ~ 0.7

Export["file.png", {"Graphics"->%}, "Rules"]

enter image description here

share|improve this answer

Perhaps this does what you want?

expr = HoldForm@Sum[Sin[i Sqrt[2]], {i, 0, 10}];

Rasterize@TraditionalForm@expr ~Binarize~ 0.7

ExportString[%, "PNG"];

ImportString[%, {"PNG", "BitDepth"}]

Mathematica graphics

Quality is really poor at screen resolution, though it would be fine at e.g. laser printer 600 dpi. (You would have to Rasterize at a larger ImageSize.)

For screen resolution use you probably want greyscale:

Rasterize@TraditionalForm@expr ~ColorConvert~ "Grayscale"

ExportString[%, "PNG"];

ImportString[%, {"PNG", "BitDepth"}]

Mathematica graphics

share|improve this answer
    
I think, Exported PDF → gs'ed PNG would be the best way to do it, but I mark your answer as accpected. It's probably as good as it gets. Thanks. –  Akater Jul 30 '12 at 12:23
    
@Akater thanks for the Accept, but it sounds like you're not really happy with this. Would you please try to explain to me again how this is lacking? It works as I thought you wanted, but maybe I don't understand. –  Mr.Wizard Jul 30 '12 at 12:30
    
@Mr.Wizard, in v8 this produces a PNG with a bit depth of 16 and RGB colorspace. To get the 1 bit output you need to use Export["file.png",{Graphics->%},"Rules"] –  Simon Woods Jul 31 '12 at 14:01
    
@Simon that's surprising. I now understand why this question was asked. Would you please either add that to my answer or (probably better) post your own? –  Mr.Wizard Jul 31 '12 at 14:14

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.