# Decode Image from Base64

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)

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"]


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