How to view an image from command line?

Might be relevant, but this is something I use that uses StartProcess and ProcessConnection.

xShow[expr_] := Module[{pr = StartProcess[{"display", "png:fd:0"}]},
  WriteLine[ProcessConnection[pr, "StandardInput"], 
    ExportString[expr, "PNG"]];
  ]

then

Plot3D[Sinh[x]Sinc[y],{x,0,Pi},{y,0,Pi}]//xShow

results in:

Screenshot

This pipes the output to the program display.

I wanted to use this as the $DisplayFunction, but that doesn't cover things like GeoGraphics or other functions that don't use $DisplayFunction, and Plot does some strange doubling (you first get a blank plot and then a plot with the function).

A clever use of $Post could be used rather than my Postfix use.


I always use Mathematica from the command line and have usually done display by setting the file init.m in Library/Mathematica/...Kernel/ to the script below. This works on a Mac; it writes the graphics to a pdf file, then displays it.

Unfortunately, in Math 11 some of the graphics displays no longer use $DisplayFunction (e.g. ListPlot, Show, ...) and I haven't found a good workaround yet. But the code below works well for Plot, Plot3D, etc.

(*-----------Graphics for command line Mathematica ----------------*)
(*  Install this file in Math../Configuration/Kernel/init.m *)
(*  It may be necessary to create this directory *)
(*  Writes images as PDF files to /tmp/math.xx; then uses Preview to show them *)

Begin["System`Private`"]  ;
MaxImages = 100;
ImageNumber = 0;
Unprotect[ $DisplayFunction]; Clear[ $DisplayFunction]; $Display = {};
$DisplayFunction := Block[{fid,str},
fid="/tmp/math."<>ToString[ImageNumber]<>".pdf";
str=Export[fid, #,"pdf"];
Run["open -a Preview "<>fid<>" &"];
ImageNumber = Mod[ImageNumber+1,MaxImages];
        #]&;
End[];

Tags:

Command Line