4
$\begingroup$

I have a Mathematica installation on a Linux server that is intended to be used for batch processing. Some of the things that will be processed will include rendering of graphics. However, if I use Export["/file/name.gif", graphicsobject], Mathematica wants to connect to an X server.

Yes, I can run Xvfb or similar, but this seems like inordinate overkill. Is this really the only option? Is there no way to get Mathematica to render to GIF (or PNG, or BMP, or TIFF, or whatever; I don't really care) without having an X server running?

Update:

I just ran this small program without an X server available:

s=Graphics[Sphere[]];
Do[
    Print[fmt];
    TimeConstrained[
        Export["/tmp/img-"<>fmt, s, fmt],
          10, Print["Timeout"] ],
  {fmt, $ExportFormats} ]

and found that the only files created that contained something other than just the text Graphics[Sphere[0, 0, 0]] or -Graphics- were DXF and JVX, both of which were very simple renderings without axes or labels or anything once anything remotely more complex was rendered.

$\endgroup$
5
  • $\begingroup$ Two things come to mind - 1 ) try postscript and some linux tool to convert 2) rasterize the graphics and write your own raw pnm format file (pnm is an easy thing to write). I have code for the later someplace, but I'll wait to see if someone has a less painful solution before going there. $\endgroup$
    – george2079
    Commented Aug 21, 2013 at 20:58
  • $\begingroup$ ..(assuming Rasterize[] doesnt need the X server.. ) I have no way to check that.. $\endgroup$
    – george2079
    Commented Aug 21, 2013 at 21:00
  • $\begingroup$ Export[]ing PostScript seems to require an X server as well. Is there another way to generate PS? And Rasterize[] requires the X server. $\endgroup$
    – wfaulk
    Commented Aug 21, 2013 at 22:26
  • $\begingroup$ I had hopes for ps (eps) since its basically the native graphics language. You might see if the content of the graphics matters, try some very simple 2d stuff with no fonts, etc. $\endgroup$
    – george2079
    Commented Aug 22, 2013 at 15:50
  • $\begingroup$ Assuming Export["/tmp/circle.eps", Graphics[Circle[]]] is simple enough, still no joy. $\endgroup$
    – wfaulk
    Commented Aug 22, 2013 at 16:12

2 Answers 2

5
$\begingroup$

I got this response from Wolfram support:

I understand you are wondering whether an X server is required to export to graphics files.

The answer is yes. Exporting to a raster format requires the front end, which in turn must be able to connect to an X server. You can accomplish this using X forwarding if you already have an X server working, or you can try setting up a virtual X server like Xvnc.

$\endgroup$
0
0
$\begingroup$

Found my raw pnm write code..

writepnm[raster_, file_] := Module[{maxval, flatdata, fmt, f},
  flatdata = Round[Flatten[raster] ];
  maxval = Max[flatdata];
  If[maxval > 255, 
    fmt = "UnsignedInteger16", 
    fmt = "UnsignedInteger8"];
  f = OpenWrite[file, BinaryFormat -> True];
  BinaryWrite[f, StringJoin[Riffle[Flatten@{"P6",
     ToString /@  Rest@Reverse@ Dimensions[raster] , 
     ToString[maxval]}, " "], {" "}], "TerminatedString" ];
  BinaryWrite[f, flatdata, fmt] ;
  Close[f];]

writepnm[Rasterize[Graphics3D[Sphere[]], "Data"], "test.pnm"]
$\endgroup$
2
  • 1
    $\begingroup$ Rasterize[] still requires an X server, unfortunately. $\endgroup$
    – wfaulk
    Commented Aug 21, 2013 at 22:22
  • $\begingroup$ i was afraid of that. $\endgroup$
    – george2079
    Commented Aug 21, 2013 at 22:43

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