Message Boards Message Boards

Displaying graphics and images inline on Wolfram Engine

I have been using Wolfram Engine for several personal projects over the last few months. Despite being free, one gets almost everything in the Wolfram Engine except for the beautiful frontend and related Dynamic functions that takes interactive computing to the next level. Because of this, it becomes a little troublesome when working with graphics and images.

When using wolfram engine on the terminal, if you try to plot a graph, you are greeted with the output --Graphics-- and this starts to become annoying after sometime. The obvious workaround is to export the expression somewhere on your file system and then use an external image viewer to see the graph. While it is not a herculean task, it becomes too distracting when doing it redundantly in a large project.

Thankfully there are projects like the Wolfram Langugae for Jupyter, Wolfram Engine JS Frontend, Wolfam Language Notebook on VS Code, etc. All these are really sophisticated frontends that provide a near Mathematica notebook experience. But personally, I cannot commit to using one of these frontends for my projects as most of them are still experimental and they tend to break at some point. I do use Jupyter Notebook sometimes when I have to present something but if the kernel hangs for some reason, it becomes hard to re-establish the connection.

For most of the time, I prefer using Wolfram Engine from the termninal and I write all my custom functions and scripts on a text editor. I use the Wolfram Engine to quickly evaluate a piece of code to verify its output and if it works as expected I trasfer the code to my text editor. This is my typical workflow. As a part of this, when I have to deal with images and grpahics, it becomes tiresome to export the output, navigate to the export directory and open the image just to get a quick view of the image/graphic output.

For sometime, I was using <<JavaGraphics which I picked up from a stackexchange post. It works alright to some extent. It displays graphics but not images (at least on my machine, not sure if this is the case with other operating systems). After a little bit more searching, I came with the perfect setup for my workflow. I discovered that there are some terminal emulators that are capable of displaying images. These terminals use sixel grahpics format to display images inline: https://www.arewesixelyet.com/

Since I am on macOS, I use iTerm2 and it has sixel support. From the documentation of iTerm2, I found that there is a utility script called imgcat that allows to display images inline: https://iterm2.com/documentation-images.html

This solved 80% of the issue. All I did next was to download the script and put it in my $PATH. Then I add a helper function qView in my init.m file. This function would take a graphics or image expression and export it a temporary directory and then use imgcat to display the image using the function Run.

qView[g_] := Module[
		{fileName},
		fileName = FileNameJoin[{
			"~",
			"tmp",
			StringReplace[
				DateString["ISODateTime"], 
				":" -> "-"
			] <> ".png"
		}];
		Export[fileName, g];
		Run["imgcat " <> fileName];
	]

And that is all. Now I can quickly view any image or graphics output right on the terminal just by postfixing the function qView. Below are some screenshots. Plotting BesselJ function A 3D plot An Image

Finally I tried plotting a list of images. I expected the output to be a rasterized list of images. But even better, surprisingly the output was an animation of images in the list.

enter image description here

To conclude, I would like to say that I have sort of created the perfect development environment for my workflow and maybe this would be helpful to someone. On Linux, the prgram img2sixel can be used similar to imgcat, however I have not tested this yet. Further ideas and suggestions are most welcome. Cheers :)

8 Replies

Thanks for this post!

Wanted to add that Kitty (alternative to iTerm2) also offers a similar feature. To obtain the same results with Kitty, one can replace Run["imgcat " <> fileName] in the module with Run["kitten icat " <> fileName].

Details could be found at https://sw.kovidgoyal.net/kitty/kittens/icat/

POSTED BY: Sudeepan Datta
Posted 5 months ago

Nice! Thanks for sharing.

You may also create a RAM disk to house your temporary directory.

POSTED BY: James Wey

Cool. I don't know if it's a bad idea or not, but you could make this even more streamlined by setting something like:

$Post = Replace[#, {Alternatives[_Graphics, _Image] :> qView[#]}] &

so that Graphics and Image expressions (and/or whatever else you want) pass through qView automatically? (i.e. saves the effort of typing //qView after each input)

POSTED BY: Josh Lietz

enter image description here -- you have earned Featured Contributor Badge enter image description here Your exceptional post has been selected for our editorial column Staff Picks http://wolfr.am/StaffPicks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!

POSTED BY: Moderation Team
Posted 1 year ago

Is it possible to rotate 3D plot and to use manipulate and animation in terminal with what you said now?

POSTED BY: Cornel B.

Hi Cornel,

I am not there yet. With the setup described here, it is only possible to display static content. Manipulate, Animate contian Dynamic functions that require the frontend.

Hi Soumya,

This is nice, thanks for sharing. BTW, you can use CreateFile[] to create a unique file in $TemporaryDirectory.

POSTED BY: Rohit Namjoshi

Hi Rohit,

Thank you for the suggestion. I will look it up and see how it can fit in.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract