At times, we as questioners would like to post huge Mathematica-expressions to websites (e.g. mathematica.stackexchange.com), or have an easy way to get such code from a site. Above a certain size, Compress yields lengthy strings, which are tedious to handle for both questioners and answerers.

Zipping might come to the rescue, however, more often than not, posting arbitrary attachments (e.g. Mathematica-notebooks or ZIP-files) is not allowed, while the full version of the expression or its Compressed variant is too cumbersome to use.

In many cases and on most sites, images are allowed, but:

How to effectively encode Mathematica expressions as images to be able to post them (and decode them afterwards)?

  • $\begingroup$ A couple of years ago I posted this idea on Meta, which was extended in cooperation with belisarius (see same page). it's basically what you posted as a self-answer here. $\endgroup$ – Sjoerd C. de Vries Feb 16 '15 at 22:18
  • $\begingroup$ Hmm. My intention was different, and the code is much simpler, but yes, there is some similarity in the idea. $\endgroup$ – Jinxed Feb 17 '15 at 7:56
  • $\begingroup$ "code simpler" . Did you see the full page? The code belisarius and I ended up at is encoding and decoding each in a single line of code each. Can't get much simpler than that. $\endgroup$ – Sjoerd C. de Vries Feb 17 '15 at 9:44
  • $\begingroup$ @SjoerdC.deVries: You are right. $\endgroup$ – Jinxed Feb 17 '15 at 10:12

This is a quite handy approach of encoding Mathematica-expressions, even complete notebooks, as grayscaled images, which can be posted as PNG-files (and decoded afterwards).

Code for encoding and decoding is as follows:

(* general encoding to grayscale data *)
seEncode[expr_] := 
  Block[{cc = ToCharacterCode[Compress[expr]], olen, a}, 
   olen = Length@cc; a = Ceiling@Sqrt@olen;
   Image[Partition[PadRight[cc, a^2], a], "Byte"]];
(* encode to PNG file directly *)
seEncode[path_String, expr_] := Export[path, seEncode@expr, "PNG"];
(* encode current notebook *)
seEncodeNotebook[] := seEncode@NotebookGet@EvaluationNotebook[];
(* encode current notebook to PNG file directly *)
seEncodeNotebook[path_String] := 
  seEncode[path, NotebookGet@EvaluationNotebook[]];
(* decode an image *)
seDecode[img_Image] := 
  With[{dec = 
     Uncompress@FromCharacterCode@Flatten[ImageData[img, "Byte"]]}, 
   If[Head[dec] === Notebook, (NotebookPut@dec;), dec]];
(* decode from image file directly *)
seDecode[path_String] := seDecode@Import@path;


  1. Encoding:

    • Call seEncode on the expression you want to share. If you specify a filename, a PNG file will be created directly.
    • Call seEncodeNotebook to encode the current notebook.
  2. Decoding:
    Call seDecode by saving or copy-pasting the (unscaled!) image from the site, or by giving a filename.
    This will reproduce the original expression (or notebook).

No tedious uploading on other (code-sharing, file-hosting, …) sites anymore!

Complete notebooks can be encoded by:

seEncodeNotebook@"<some path you choose>";

Example of output (a notebook containing only the code given above):

Example encoded notebook containing the functions mentioned.

and decoded accordingly using (after having saved the posted image to disk first for safety):

seDecode@"<path to the downloaded image file>";

I hope, that this might be of some use to you all!

  • $\begingroup$ There is no need for the decoding parameter - any characters added at the end of the compressed string will be ignored by Uncompress $\endgroup$ – Simon Woods Feb 16 '15 at 20:58
  • $\begingroup$ I would also suggest using Hold or Defer as the second argument in Uncompress, for safety. Consider seEncode@Unevaluated@Unevaluated@Print["oops"] but with something nasty in place of the print. $\endgroup$ – Simon Woods Feb 16 '15 at 21:16
  • $\begingroup$ @SimonWoods: I fixed the superfluous parameter and added some more convenience to the functions. $\endgroup$ – Jinxed Feb 17 '15 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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