5
$\begingroup$

From a web API, I have a bunch of realllyyy long strings that look like this:

data:image/jpeg;base64,(...)

Sites like this will decode these strings no problem, but Mathematica doesn't seem to be able to. BaseDecode gives me a ByteArray which can't be re-imported as a JPEG, and ImportString doesn't want to import as a JPEG either.

Is there any way, in Mathematica, to convert from a base64 encoded image to the image itself?

For experimentation, here is a dump of one of the image strings. It should end up looking like a semi-messed up cathedral thingy (I'm training GANs)

$\endgroup$
7
$\begingroup$

You just got to remove the initial data:image/jpeg;base64,, everything after that is base64 encoded pixel data.

data = Import["https://pastebin.com/raw/KHE6A3gR", "Text"];
data = StringReplace[data, "data:image/jpeg;base64," -> ""];
ImportString[data, "Base64"]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Oh nice! I assumed it would need that to infer the type. Thank you! $\endgroup$ – TreFox Oct 7 at 16:51

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.