9
$\begingroup$

While tinkering with the code in the Falling Matrix question, I was hoping to improve the rendering speed by making the rasterizations of the text grayscale. I changed the base color of the text to white and left the background black. However, this did not yield a grayscale raster. The problem seems to be that Mathematica tries to help with the readability of text by using special antialiasing schemes.

Consider the following test:

GraphicsRow[
 Rasterize[#, Background -> Black, RasterSize -> 15, 
    ImageSize -> 150] & /@ {Style["\[FilledCircle]", White], 
   Graphics[{White, Disk[]}]}]

Antialiased disk test

Interesting and (I think) undocumented, but the relevant question for me is: How can I control or eliminate this behavior? Adding a ColorConvert[#,"Grayscale"] or just taking the Norms of the ImageData gives an acceptable result but is pretty slow.

$\endgroup$
1
  • $\begingroup$ I can confirm on mma 9.0.0 & 8.0.4 on win7 $\endgroup$
    – Ajasja
    Mar 4, 2013 at 18:45

3 Answers 3

8
$\begingroup$

One possible way would be to extract the text outline as in this answer (scroll to the end) and rasterize the resulting graphics. First, define a convenient function:

textGraphics[t_String | t_Style] := Graphics@First@First@
    ImportString[ExportString[t, "PDF"], "PDF", "TextMode" -> "Outlines"];

We can see this in action:

textGraphics /@ {Style["A", FontSize -> 12, FontColor -> Red, FontFamily -> "Times"], "B"} // Row

If you now try your example above and apply this function on the styled text, you should get this:

$\endgroup$
2
  • $\begingroup$ There seems to be a bit of a problem when using this for characters that do not fill much of the monospace-character box. You could use an explicit PlotRange to restore it, but is there some a priori way to know what the correct bounding box will be? $\endgroup$
    – Xerxes
    Mar 4, 2013 at 21:58
  • $\begingroup$ @Xerxes Perhaps something like this: mathematica.stackexchange.com/a/4162/5 $\endgroup$
    – rm -rf
    Mar 4, 2013 at 22:01
5
$\begingroup$

First of all, the most practical solution is probably @rm's.

Mathematica simply uses your operating system's font renderer. If you disable subpixel antialiasing at the operating system level, it will be disabled in Mathematica too. It is possible to do this on all of Windows, OS X and Linux, but the procedures are different. However, you may not want to do this permanently as it's going to reduce the readability of text in every program.

enter image description here

$\endgroup$
4
$\begingroup$

This is closely related to my own question: Poor anti-aliasing in Rotated text with ClearType on.

EDIT: This apparently only works in Windows systems.

The same solution, Opacity of anything other than one, can be applied:

GraphicsRow[
 Rasterize[#, Background -> Black, RasterSize -> 15, ImageSize -> 150] & /@ {Style[
    "\[FilledCircle]", White, Opacity[0.9999]], Graphics[{White, Disk[]}]}]

Mathematica graphics

Opacity[2] also works on my system.

$\endgroup$
11
  • $\begingroup$ This does not seem to work for me (v9 Linux) using Opacity[2], Opacity[0.999] or FontOpacity->0.999. $\endgroup$
    – Xerxes
    Mar 4, 2013 at 21:24
  • $\begingroup$ @Xerxes I'm sorry to hear that. It does work on Windows. I hope you find a solution; if you do please post an answer for my question too. $\endgroup$
    – Mr.Wizard
    Mar 4, 2013 at 21:26
  • $\begingroup$ It doesn't work on OS X v9 either. Just include a note that this is a solution for Windows :) (and +1) $\endgroup$
    – Szabolcs
    Mar 4, 2013 at 21:31
  • $\begingroup$ @Xerxes Out of curiosity what happens if you Rotate the text by 45° before rasterizing? $\endgroup$
    – Mr.Wizard
    Mar 4, 2013 at 22:13
  • $\begingroup$ @Szabolcs Same question to you, please. $\endgroup$
    – Mr.Wizard
    Mar 4, 2013 at 22:13

Your Answer

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

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