# HOWTO encode/decode large chunks of Mathematica code/results e.g. for posting on sites?

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)?

• 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. – Sjoerd C. de Vries Feb 16 '15 at 22:18
• Hmm. My intention was different, and the code is much simpler, but yes, there is some similarity in the idea. – Jinxed Feb 17 '15 at 7:56
• "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. – Sjoerd C. de Vries Feb 17 '15 at 9:44
• @SjoerdC.deVries: You are right. – 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;
(* 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"]]},
(* decode from image file directly *)
seDecode[path_String] := seDecode@Import@path;


# Usage

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).

Complete notebooks can be encoded by:

seEncodeNotebook@"<some path you choose>";


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

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!

• There is no need for the decoding parameter - any characters added at the end of the compressed string will be ignored by Uncompress – Simon Woods Feb 16 '15 at 20:58
• 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. – Simon Woods Feb 16 '15 at 21:16
• @SimonWoods: I fixed the superfluous parameter and added some more convenience to the functions. – Jinxed Feb 17 '15 at 7:52