# How do I flatten transparency on a graphics, for conversion to eps or similar?

I've got a graphic that I'd like to save as an eps file. Trouble is, the graphic has some transparency to it. Since the eps format doesn't handle transparency, I want to flatten the transparency out, effectively making an image with no transparency on a fixed (white) background that looks like the original transparent image.

To give some background: To make my original image, I started with a plot, combined it with some background grids, and rasterized the result with Background->None. I then combined the image with an image of (vector) grid axes. I do this because I need the plot rasterized because it's built from a very large data set, but I need the axes vectorized because I need them to appear in a LaTeX document. I set Background->None in order to keep the plot's white background from blotting out the axes, and I combined the original plot with background grids so that the grids can be seen behind the image when rasterized.

Currently when I try to save the document as an eps it gets rid of the transparency in the rasterized plot, which ends up meaning that the grids can no longer be seen through it. I'd like to be able to save as an eps and "flatten" the transparency, so that the image looks like it does with the transparency without the document actually using transparency (since eps can't handle transparency). Alternatively, if I could save it as a pdf but still get rid of the transparency in a similar way that would also be acceptable. I need to save the document in a vector format to keep the axes looking right.

In case anyone is curious, here is my code. RatioPlotData is a big (~2,000 points) array.

SetDirectory[NotebookDirectory[]];
SetOptions[\$FrontEnd, PrintingStyleEnvironment -> "Working"];
in = 72;
G3D = Graphics3D[AlignmentPoint -> Center, AspectRatio -> 0.925,
Axes -> {True, True, True},
AxesEdge -> {{-1, -1}, {-1, -1}, {-1, 1}},
AxesStyle -> Directive[10, Black],
BaseStyle -> {FontFamily -> "Arial", FontSize -> 12},
Boxed -> False, BoxRatios -> {3, 3, 1},
LabelStyle -> Directive[Black], ImagePadding -> 40,
ImageSize -> 5 in, PlotRangePadding -> 0,
TicksStyle -> Directive[10], ViewPoint -> {-2, -2, 2},
ViewVertical -> {0, 0, 1}];
(*axesLabels=Graphics3D[{Text[Style["v",Black,12],Scaled[{.5,-.15,0}],\
{0,0},{1.,0}],Text[Style["u",Black,12],Scaled[{-.15,.5,0}],{0,0},{1.,\
0}],Text[Style["Subsuperscript[R, 6, (3)]/Subsuperscript[R, 6, \
(2)]",Black,12],Scaled[{-.2,1.15,.6}],{0,0},{1.0,0}],Text[Style[\
"Collinear \
Limit",ColorData[1,2],12],Scaled[{0.5,0.5,-0.2}],{0,0},{1.0,0}]}];*)
fig = Show[
ListPlot3D[
Join[Append[RatioPlotData, {1, 1, -7.004088513718098}],
Join[Transpose[Prepend[Transpose[ratlinepoints], Table[1, {19}]]],
Transpose[{Transpose[ratlinepoints][[1]], Table[1, {19}],
Transpose[ratlinepoints][[2]]}]]],
PlotRange -> {{0, 1}, {0, 1}, {-7.35, -7}}, ClippingStyle -> None,
Mesh -> {{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}, {0.1, 0.2,
0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9}}, MeshStyle -> Gray,
PlotStyle -> Opacity[0.8](*,AxesLabel->{"v","u","R63/R62"}*)],
ImagePadding -> {{70, 10}, {15, 0}}, Options[G3D]]
(*collline=Show[ParametricPlot3D[{u,1-u,-7.35},{u,0,1},PlotStyle->{\
ColorData[1,2],Dashed,Thick}],AbsoluteOptions[fig]]*)
axes = Show[
Graphics3D[{},
FaceGrids -> {{{1, 0,
0}, {{0, 0.2, 0.4, 0.6, 0.8,
1}, {-7.0, -7.1, -7.2, -7.3, -7.35}}}, {{0, 1,
0}, {{0, 0.2, 0.4, 0.6, 0.8,
1}, {-7.0, -7.1, -7.2, -7.3, -7.35}}}, {{0,
0, -1}, {{0.2, 0.4, 0.6, 0.8}, {0.2, 0.4, 0.6, 0.8}}}},
AbsoluteOptions[fig]](*,axesLabels,collline*)];
fig = Show[fig,
FaceGrids -> {{{1, 0,
0}, {{0, 0.2, 0.4, 0.6, 0.8,
1}, {-7.0, -7.1, -7.2, -7.3, -7.35}}}, {{0, 1,
0}, {{0, 0.2, 0.4, 0.6, 0.8,
1}, {-7.0, -7.1, -7.2, -7.3, -7.35}}}, {{0,
0, -1}, {{0.2, 0.4, 0.6, 0.8}, {0.2, 0.4, 0.6, 0.8}}}}];
fig = Show[fig, AxesStyle -> Directive[Opacity[0]]];
Row[{fig, axes}]
fig = Magnify[fig, 5];
fig = Rasterize[fig, Background -> None];
axes = First@ImportString[ExportString[axes, "PDF"], "PDF"];
result = Show[axes,
Epilog -> Inset[fig, {0, 0}, {0, 0}, ImageDimensions[axes]],
Background -> White];
result

• When you say "flatten the transparency out" do you mean you want to remove or alter parts of the plot that have the opacity set at less than 1? – Mike Honeychurch Jul 24 '13 at 22:06
• You could try this answer to export your plot as eps which will invoke ghostscript to perform the flattening. Not sure if this works for your case - let me know if it does. – Jens Jul 24 '13 at 22:32
• You can use Adobe Illustrator for this: export your fugure as PDF or SVG, then open it in Illustrator and save as EPS (you will probably need to turn off the "Include CMYK PostScript in RGB Files" option to preserve original colors). All the transparent objects will be properly flattened. – Alexey Popkov Jul 25 '13 at 6:09
• You could use Overlay instead of Show to render the vector graphic on top of the Rasterized graphic. The disadvantage is that Overlay is actually 'low-level overlay', it really just stacks graphics on top of each other no matter what they show, so you would have to align the plot and axes manually (e.g. ensuring the same plot range). – Theo Tiger Jul 25 '13 at 20:17
• What might be helpful to use the Overlay method is to look at the FullForm of the Show result. Perhaps you can use the options from it to render your plots separately, perhaps you might even want to work with the Graphics primitives produced by Show ... try it out. I don't see a more direct solution to your question so far. – Theo Tiger Jul 25 '13 at 20:27

Here is a quick introduction to the problem of transparency flattening. In short, EPS (Encapsulated PostScript) format "as is" does not support transparency (while PDF format supports it). It means that a figure containing transparent objects must be converted into another figure which does not have transparent objects but looks exactly the same in order to export it as proper EPS file.

Mathematica currently is unable to do transparency flattening. Instead, it either rasterizes the whole figure or just strips out all the information about transparency when exporting figure as EPS, and produces corrupted figure as a result. Unfortunately Mathematica does not warn the user that it cannot export the figure properly (when exporting to a vector format it is expected to obtain a vector figure from a vector graphics, after all!). It can correctly export Graphics with transparency only to PDF and SVG. Before version 10 Mathematica also correctly exported Graphics3D in vectors as PDF (even with transparency) but unfortunately starting from version 10 this functionality is disabled by default and Export produces a PDF with rasterized version of the whole figure (again without any warning).

But actually the situation is even worse: there are not too many third-party applications which are able to flatten vector transparency without rasterizing all the transparent objects. At the moment I know only Adobe Illustrator and Adobe Acrobat. I was unable to force CorelDraw to flatten transparency without rasterizing it, and Inkscape, GhostScript and Poppler work in the same way.

By default Acrobat rasterizes transparent objects when exporting a PDF file as EPS. But if one sets the "sRGB IEC61966-2.1" color profile and turns off color separation in the "Save As... -> Settings" dialog, all the transparent vector objects are converted into many simple vector forms without transparency but looking the same. Unfortunately generated EPS file often has low compatibility with other programs.

Illustrator by default also rasterizes transparent objects and the solution is to turn off the "Include CMYK PostScript in RGB Files" option in the EPS export dialog. EPS files generated by Illustrator have high compatibility, can be imported in Microsoft Word.

I am still looking for other (especially free) ways to flatten transparency in PDF files without rasterization.

### UPDATE

Illustrator is able to Place EPS and PDF figures inside of a document without importing them. Then the document can be saved as PDF with transparency flattening applied without rasterization if one sets in the Save Adobe PDF dialog in Illustrator Compatibility: Acrobat 4 (PDF 1.3), as demonstrated in this answer. For saving as EPS without rasterization it is necessary that the document has RGB colorspace and in the EPS Options` export dialog the option "Include CMYK PostScript in RGB Files" should be turned off: