7
$\begingroup$

Here I produce a Raster image with large uniformly colored rectangles and put it in an Inset:

With[{n = 5},
 Graphics[Inset[
   Graphics[Raster[
     {Map[
       List @@ ColorConvert[ColorData["LakeColors"][#], 
          RGBColor] &, (Range[n - 1] - 1)/(n - 2)]}
     ]],
   Automatic, Automatic, {.45, .5}
   ]
  ]
 ]

Raster

Now export this to PDF:

Export["p.pdf", %]

ramp artefacts

The uniform regions have been replaced by a series of discrete bands that seem to try an interpolate between the rectangles. This happens if I choose fewer rectangles too (e.g., n = 3).

If I generate the same thing without using Raster, the exported PDF looks fine - e.g., try this:

With[{n = 5},
 Graphics[Inset[
   Graphics[
    MapIndexed[{ColorData["LakeColors"][#], 
       Translate[Rectangle[], {#2[[1]], 0}]} &, (Range[n - 1] - 
        1)/(n - 2)]
    ]
   ,
   Automatic, Automatic, {.45, .5}
   ]
  ]
 ]

So it's the conversion of the rasterized image in Export that's responsible for this effect. Interestingly, one can work around this (on Mac) by selecting the graphic, going to Edit > Copy As > PDF and pasting the result into Preview.

Although I have some workarounds now, it still seems mysterious to me why this banding effect happens with Raster. And it would of course be nice if someone knew how to use Raster in an Inset inside a Graphics without triggering the effect that causes the blocks of the raster to lose their uniform color.

Also - the arguments to Inset can be left out (in particular the scaling {.45, .5}). But it seemed to me that this problem didn't always happen with Raster, and that it probably has something to do with the size of the bitmap being scaled up relative to some assumed coordinate system used in Export.

$\endgroup$
6
  • $\begingroup$ Yes, I can confirm that this happens on Mac.The problem is Preview. If you load the file in Adobe Reader you will see the PDF graphic as expected. $\endgroup$
    – Matariki
    Commented Jul 10, 2012 at 0:39
  • $\begingroup$ Yes, I've seen this problem too on both Mathematica and MATLAB outputs, and as Matariki said, it's due to Preview. The PDF/EPS files look fine when viewed with Adobe Reader or GhostView $\endgroup$
    – rm -rf
    Commented Jul 10, 2012 at 0:56
  • $\begingroup$ @Matariki Thanks for the pointer - I guess this means it's Mac specific. Then I'll probably have a look at the PDF source and see if I can come up with a fix along the lines of the MediaBox replacement I did on this page - it didn't occur to me that I'd have to revisit that in this context... $\endgroup$
    – Jens
    Commented Jul 10, 2012 at 1:35
  • $\begingroup$ @Jens I don't know if it is Mac specific but more Preview specific as the effect is not visible using PDF Reader on the Mac. This might have to do with OSX PDF rendering engine. I never looked into it myself. I couldn't observe the effect on Windows but there I used PDF Reader as the default. $\endgroup$
    – Matariki
    Commented Jul 10, 2012 at 2:04
  • $\begingroup$ @Matariki Yes indeed - it's WebKit specific. ghostscript and Inkscape work perfectly with the file. I meanwhile figured out a good work-around: apply ColorConvert[..., "CMYK"] to the Graphics[Raster[...]] expression, and the output looks fine. I want to try and see if one can do even better, though... $\endgroup$
    – Jens
    Commented Jul 10, 2012 at 2:13

1 Answer 1

7
$\begingroup$

This issue seems to affect all PDFKit based viewers on OS X, but it doesn't appear to point to anything intrinsically wrong with Mathematica's PDF export. A work-around that allows you to view the exported PDF without color bands in PDFKit viewers such as Preview would be to use the following command when creating the PDF in Mathematica:

Export["py.pdf", 
 p /. Graphics[Raster[i__]] :> 
   Image@Graphics[Raster[i]]]

Alternatively, if you know exactly where the Raster is being created, you may as well do the conversion immediately at that point. Then my example code above would have to be modified as follows:

With[{n = 5},
 Graphics[Inset[
   Image@Graphics[Raster[
      {Map[
        List @@ ColorConvert[ColorData["LakeColors"][#], 
           RGBColor] &, (Range[n - 1] - 1)/(n - 2)]}
      ]]]
  ]
 ]

After doing this, I see no more trace of the discrete bands.

Edit Originally I had inserted ColorConvert but then realized it's simply the conversion to Image that is required to make it work.

Edit 2

To make this more generally applicable: whenever your exported PDF shows banding in gradients, you could try to export the plot (let's call it plot) using this command:

Export["plot.pdf", plot /. Raster[i__] :>
 Raster@ImageData@Image@Graphics[Raster[i]]]

Edit 3

This is an old post, but in the meantime I encountered similar issues in many other contexts and came up with a different work-around which also applies here:

rasterTrick[plot_] := 
 Show[plot, 
  Prolog -> {Opacity[0], Texture[{{{0, 0, 0, 0}}}], 
    VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}}, 
    Polygon[{{0, 0}, {.1, 0}, {.1, .1}}]}]

Export["p.pdf", plot // rasterTrick]

The function rasterTrick was recently useful in answering this question (where you can also find a link to further explanations of how it works): Export MatrixPlot graphics to PDF

$\endgroup$
2
  • $\begingroup$ Well done, Jens. Dafuer gibt's +1 $\endgroup$
    – Matariki
    Commented Jul 10, 2012 at 2:37
  • $\begingroup$ @Matariki And thanks for your help finding the culprit! $\endgroup$
    – Jens
    Commented Jul 10, 2012 at 2:39

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.