Exporting a plot as an image in Mathematica on a machine without X11 (ie, without involving the frontend)
-
10-07-2019 - |
Question
Consider the following dialog with the command-line interface to the kernel:
$ math
Mathematica 6.0 for Linux x86 (32-bit)
In[1]:= p = Plot[x^2, {x,-1,1}]
Out[1]= -Graphics-
In[2]:= Export["foo.png", p]
Out[2]= foo.png
That works fine on a machine with $Version = 6.0 for Linux x86 (32-bit) (June 2, 2008)
but fails on a machine with $Version = 7.0 for Linux x86 (64-bit) (November 11, 2008)
with the following error:
Export::nofe: A front end is not available; export of PNG
requires a front end.
With similar errors for any other image format I can think of.
So the question is, how can I get the Mathematica kernel, sans front end, to export images? Why does it work without a hitch in Mathematica 6.0? If the above example works for you in version 7, please let me know!
PS: Version 7 introduced the function UsingFrontEnd
but that fails with
Developer`UseFrontEnd::nofestart:
Unable to launch a front end. Proceeding without a front end.
presumably because X11 is not installed on the machine.
Addendum
It turns out there is no difference between version 6 and version 7 in this regard. Rather, on the machine with version 6, the front end was being invoked silently. The problem with the other machine, as the answers to this question make clear, is that there was no X server and so the front end could not be invoked.
Solution
You need a front end to export graphics from the MathKernel. The rendering of graphics are entirely covered by the frontend. Without a Front End you do not have graphics to export. There is no workaround.
What you can do, if you have a working front end, is to use:
UseFrontEnd[Export[filename,graphics]]
This will invoke the front end from the terminal, allowing you to export the graphics.
To have an working front end on Linux machine without X server and windows manager installed you have the following options:
Use X display from some other machine. See e.g. http://www.hungry.com/~jamie/xexport.html. Basically it boils down to setting the environmental variable
export DISPLAY=machine_address:0.0
or passing during each execution option-display machine_address:0.0
.Install locally Xvfb which is a X server frame-buffer. It is pretty simple and straightforward to use. See e.g. Wikipedia entry on Xvfb. You can set X display when running Xvfb
Xvfb :display_nr &
and then use it as in previous option.
OTHER TIPS
Exporting graphics requires the front end in version 6, too. In turn, the front end might require X (after all even some of the 2D plots require 3D graphics acceleration ...)
I only have v6 for Windows. There the front end is automatically started up in the background as needed, when exporting graphics from a command line session.