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


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)


1 Answer 1


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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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