Skip to main content

export - Embed Code Into Image


I often export images(plots, matrix, arrays, etc..) from mathematica which I end up putting in word documents or uploading to the web. The problem is that I often lose the original code and I am left only with the image representing the original output.


I was thinking/considering of using one of the libraries discusses here https://stackoverflow.com/questions/3335220/embed-text-into-png but was wondering what the Mathematica community new of any functionality built into Mathematica allowing to embed text(the code) into the image.


Compatibility Table For Answers


Edit: For documentation purposes this is the code I personally use. It varies slightly from the other answers because it embeds that data into the images pixels.



Answer



Here a quick hack for PNG images. As its Wikipedia page shows the format works with coded chunks and you can make up and insert chunk types yourself. I'm not sure how safe it is to add beyond the official end of file marker as Simon Woods suggests in his answer. It seems like a breach of the standard to me.



The following code, which more closely seems to follow the PNG standard, inserts a "mmAc" (Mathematica code) chunk before the end of file marker. A chunk consists of a four byte length coding, a four byte chunk name, the content itself and a four byte CRC32 check.


ClearAll[myGraphicsCode];

SetAttributes[myGraphicsCode, HoldFirst];

myGraphicsCode[gfun_, opts__: {}] :=
Module[{img, pngData, extraData},
img = Image[gfun, FilterRules[opts, Options[Image]]];
pngData = Drop[ImportString[ExportString[img, "PNG"], "Binary"], -12];
extraData = ToCharacterCode@Compress@Defer@gfun;

Join[pngData,
IntegerDigits[Length[extraData], 256, 4],
ToCharacterCode@"mmAc",
extraData,
IntegerDigits[
Hash[StringJoin["mmAc", FromCharacterCode@extraData], "CRC32"],
256, 4
],
{0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}
]

]

Please note that the specific capitalization of the chunk name used here is essential.


Generating the image:


Export[
"C:\\Users\\Sjoerd\\Desktop\\Untitled-1.png",
myGraphicsCode[
Plot[Sin[ x^2], {x, -3, 3}],
ImageResolution -> 100
],

"Binary"
]

Posting it here:


enter image description here


Getting the plot information from the image posted above:


Import["http://i.stack.imgur.com/4bEXu.png", "Binary"] /. 
{___, a : PatternSequence[_, _, _, _], 109, 109, 65, 99, b___} :>
Uncompress@FromCharacterCode@Take[{b}, FromDigits[{a}, 256]]



Plot[Sin[x^2], {x, -3, 3}]



Some image editors respect the chunk, others don't. Here is a vandalized version of the above file (done in MS Paint):


enter image description here


It still works:


Import["http://i.stack.imgur.com/eA1CS.png", "Binary"] /. 
{___, a : PatternSequence[_, _, _, _], 109, 109, 65, 99, b___} :>
Uncompress@FromCharacterCode@Take[{b}, FromDigits[{a}, 256]]



Plot[Sin[x^2], {x, -3, 3}]



I tested it in Photoshop 10.0.1, but it unfortunately didn't work there.




UPDATE 1


As requested by Stefan, here a step by step explanation how it's done. I'll use an update version of the above code that I used to investigate ajasja's suggestion of using standard public chunck names instead of custom ones. This to see whether Photoshop respects those (it doesn't either).


Attributes HoldFirst is set so that I can enter plot code without having it evaluated prematurily.


ClearAll[myGraphicsCode];
SetAttributes[myGraphicsCode, HoldFirst];


I want to be able to flexible set the bitmap properties of the plot. So I allowed for the options of Image to be passed through my function.


myGraphicsCode[gfun_, opts__: {}] :=
Module[{img, pngData, extraData},
img = Image[gfun, FilterRules[opts, Options[Image]]];

I use ExportString to export the image as a PNG to string data. This saves me temporary file handling. The image is immediately imported again, but now as a list of bytes. Mathematica closes the PNG with a standard 12 byte sequence ({0,0,0,0} (data length)+"IEND"+CRC). I chop it off and will add it back later on.


  pngData = Drop[ImportString[ExportString[img, "PNG"], "Binary"], -12];

Here the stuff for a "iTXt" chunk (see the W3 PNG definition for details):



  extraData = 
Join[ToCharacterCode@"iTxtMathematica code", {0, 0, 0, 0, 0},
ToCharacterCode@Compress@Defer@gfun];

I wrapped the plot code with Defer so that it won't be evaluated once recovered from a file's meta data. Compress converts it to a safe character range and does some compression.


Putting it all together. IntegerDigits[value, 256, 4] turns value into 4 bytes. 4 is subtracted because the length should not include the chunk name.


  Join[pngData, IntegerDigits[Length[extraData] - 4, 256, 4], 
extraData,

Now, the CRC32 hash is calculated and also turned into a four-byte sequence. Note that both Photoshop and MS Paint don't seem to check this. Quicktime's ImageViewer OTOH does check it and can be used therefore to verify your code. Finally, the end marker is added back.



   IntegerDigits[Hash[FromCharacterCode@extraData, "CRC32"], 256, 4], 
{0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}]
]

Code for importing the meta data:


codeFinder := {___, a : PatternSequence[_, _, _, _], Sequence @@ 
ToCharacterCode@"iTXtMathematica code", b___} :>
Uncompress@FromCharacterCode@Take[{b}, {5, FromDigits[{a}, 256]}]

Import["C:\\Users\\Sjoerd\\Desktop\\Untitled-1.png", "Binary"] /. codeFinder


Note that I import as binary. I don't want and need any image conversion. What follows is a bit of pattern matching. The core of which is the chunk name "iTXt" and the keyword "Mathematica code" that I wrote into the file earlier.


The preceding a : PatternSequence[_, _, _, _] is used to catch and name the 4 length bytes. After conversion with FromDigits again, this is used to take a precise bite out of the data from the remainder of the file that was put into b. FromCharacterCode converts it to a string again, which is then returned into readable Mathematica code by Uncompress.




UPDATE 2


I tested importing graphics from Word documents. I added the above picture to a DOCX and used the following:


Import[
"C:\\Users\\Sjoerd\\Desktop\\Doc1.docx",
{"ZIP", "word\\media\\image1.png", "Binary"}
] /. codeFinder



Plot[Sin[x^2], {x, -3, 3}]



Works without a hitch.


Internal file names used by Word can be found thus:


Import["C:\\Users\\Sjoerd\\Desktop\\Doc1.docx"]


{"[Content_Types].xml", "_rels\.rels", \ "word\_rels\document.xml.rels", "word\document.xml", \ "word\theme\theme1.xml", "word\media\image1.png", \ "word\media\image2.gif", "word\settings.xml", \ "word\webSettings.xml", "word\stylesWithEffects.xml", \ "word\styles.xml", "docProps\core.xml", "word\fontTable.xml", \ "docProps\app.xml"}




Which is where I found my PNG file imported above.


Comments

Popular posts from this blog

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1....