2
$\begingroup$

My goal is to get a Base64 string directly from a plot (or any Graphics object) in Mathematica such that, when using a Base64 decoder online, such as this one, I get back the image of my plot.

How can I do this?

$\endgroup$
  • 2
    $\begingroup$ Maybe something like this?: s = ExportString[Compress[im], {"Base64", "Text"}, CharacterEncoding -> "ISO8859-1"]; Uncompress@ ImportString[s, {"Base64", "Text"}, CharacterEncoding -> "ISO8859-1"] -- not completely sure what you're after. $\endgroup$ – Michael E2 Jan 3 at 18:05
  • $\begingroup$ How about str = ExportString[im, {"Base64", "WDX"}]? $\endgroup$ – chuy Jan 3 at 18:57
  • 4
    $\begingroup$ What are you trying to do this for? Is there any importance of "Base64"? If you just need to represent the plota as a string to be reloaded in Mathematica, using Compress and Uncompress will work pretty well... $\endgroup$ – Albert Retey Jan 3 at 20:09
  • $\begingroup$ I think this question can't be answered productively before Albert's comment is addressed. It looks like an XY problem to me. $\endgroup$ – Szabolcs Jan 4 at 11:58
  • 1
    $\begingroup$ I rewrote the question to make it clear what you want to do. Please edit if needed. $\endgroup$ – Szabolcs Jan 11 at 20:40
6
$\begingroup$

Online services, such as the one referenced in the question, require the image to be in one of the standard web formats (PNG, JPEG, etc.)

First you must export the graphics into one of these image formats. This gives you a string of bytes (a file). Base64 is a way to encode a string of arbitrary bytes into a string of ASCII characters.

As Kuba mentions, the entire process can be done in one step as

pl = Plot[Sin[x], {x, 0, 10}]

base64 = ExportString[pl, {"Base64", "JPEG"}]

Now copy it and use it with the online decoder:

CopyToClipboard[base64]
$\endgroup$

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