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;
Usage
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.
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):

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!