How to Convert Base64 to an Image (and an Image to Base64)
Turn a Base64 string or data:image URI into a downloadable PNG, JPG or GIF, or turn an image into Base64. Fix truncated strings, bad characters and more.
Updated November 7, 20267 min read
To turn Base64 into an image, paste the string into the Base64 decoder. It strips any data:image/png;base64, prefix, decodes the bytes, works out from the first few bytes that the result is a PNG, JPEG or GIF, shows you a preview and lets you download it with the right extension. To go the other way, drop the picture into the Base64 encoder and switch on Wrap as a data URI. The rest of this guide covers the details that decide whether it works: what to paste, what the errors mean, and when embedding an image as text is a bad idea. For what Base64 is in the first place, see what is Base64 encoding.
What you are holding: a bare string or a data URI
Images turn up as text in two shapes. A bare Base64 string is just the characters, often starting with iVBORw0KGgo (a PNG) or /9j/ (a JPEG). A data URI is the same characters with a label in front, such as data:image/png;base64, followed by the string. Browser developer tools, CSS files, HTML emails and API responses mostly give you the second shape. The decoder accepts both, so you never need to cut the prefix off by hand.
| What you pasted | Looks like | What the decoder does |
|---|---|---|
| Bare string | iVBORw0KGgoAAAANS… | Decodes it and identifies the file from its bytes |
| Data URI | data:image/jpeg;base64,/9j/4AAQ… | Removes the prefix, then decodes the rest |
| URL-safe string | Uses - and _ instead of + and / | Recognised automatically, padded or not |
| Wrapped lines | Broken into lines of 64 or 76 characters | Line breaks are ignored in the default mode |
Converting Base64 to an image, step by step
Open the decoder
Go to the Base64 decoder. Leave Input format on Auto so it can detect a data URI by itself.
Paste the string
Paste the whole thing, prefix included, into the Base64 box. Anything up to 256 KB decodes as you paste; for larger input, press Decode. You can also drop a .txt or .b64 file that contains the string.
Check the preview and the Detected line
The result panel shows the format it found, the decoded size in bytes and, for images, a preview. If the Detected line says PNG, JPG, GIF, WebP, AVIF or similar, you have a real image.
Download
Press Download. The file is saved with the extension its own bytes call for, so a decoded PNG is saved as .png and opens with a double click.
Paste a string or data URI and download the image. Free: up to 25 MB of Base64, one input at a time, every format and view included. Pro: up to 20 inputs at once, downloaded as one ZIP. It all runs on your device, not our servers.
Decode Base64 to an imageHow the image type is detected
Base64 says nothing about what it contains, so the tool reads the signature that almost every binary format starts with. Among images it recognises PNG, JPEG, GIF, WebP, BMP, ICO, TIFF, AVIF and HEIC. It also recognises PDFs, ZIP-based files such as DOCX, audio and video, and fonts, so you learn quickly if you were handed something other than a picture.
| Decoded bytes begin with | Detected as | Saved as |
|---|---|---|
| 89 50 4E 47 (the letters PNG) | PNG image | .png |
| FF D8 FF | JPEG image | .jpg |
| GIF87a or GIF89a | GIF image | .gif |
| RIFF … WEBP | WebP image | .webp |
| An opening <svg tag | SVG, a text format | .svg |
| Nothing recognised, no NUL bytes | Plain text | .txt |
SVG is the odd one out. It is text, not binary, so it is identified from its opening tag and offered as an .svg file rather than shown as a picture preview. If a string decodes to readable XML mentioning svg, save it and open it in a browser.
Converting an image to Base64 or a data URI
Open the encoder
Go to the Base64 encoder and drop your image where it says to drop a file. Free handles files up to 25 MB, one at a time.
Choose the shape
Turn on Wrap as a data URI if the string is going into HTML or CSS. The media type is read from the file's own bytes and added to the prefix. A data URI has to be one unbroken line with the standard alphabet and padding, so the tool switches off URL-safe, wrapping and per-line mode for you and tells you it did.
Encode and copy
Press Encode, then copy or download the result. For a very large result only the first 32,768 characters are previewed on screen, but Copy and Download always give you the whole string.
Leave the data URI switch off when you want only the raw string, for example to put into a JSON field that an API will decode itself. If you want both directions on a single page, the combined Base64 encoder and decoder has them.
Common errors and how to fix them
When a string cannot be decoded, the tool does not just say invalid. It names the character position, the line and column, and the character itself, which is what lets you fix a string with thousands of characters.
| Message | Usual cause | Fix |
|---|---|---|
| Not valid Base64 | A stray character such as a quote, a comma or a non-breaking space picked up when copying | Go to the reported position and delete the character |
| The string is truncated | Part of the string was cut off, often by a chat app, a spreadsheet cell or a text limit | Copy the full value again from the source |
| Data after the = padding | Two Base64 strings were joined together | Split them and decode each one separately |
| Decodes but is not an image | The string holds a PDF, a ZIP or plain text | Read the Detected line and use the matching format |
URL-safe strings and whitespace
Some systems swap + and / for - and _ so the string can sit in a URL. You do not need to convert it back: the decoder recognises both alphabets, with or without the = padding at the end. Spaces and line breaks are stripped before decoding. Missing padding is added for you unless you turn on Strict mode, which refuses malformed input instead of repairing it. Leave strict mode off for ordinary recovery work.
One trap is a string that arrived percent-encoded, with %2B and %3D in place of + and =. That is a second layer of encoding on top. Run it through the URL decoder first, then decode the Base64.
When embedding an image as Base64 is a bad idea
Base64 writes four characters for every three bytes, so the text is about a third larger than the file, before the prefix and any line breaks are counted. A 1 MB image becomes roughly 1.37 MB of text. That is a fair price for a 2 KB icon and a poor one for a photograph.
| Situation | Embed as Base64? | Why |
|---|---|---|
| Small icon or bullet used on one page | Yes | Saves a request and nothing is large |
| Image in an email signature or Markdown file | Often yes | There may be nowhere to host a file |
| Photo or hero image | No | About 33% heavier, and the page cannot show anything until it has been downloaded |
| Image used on many pages | No | A separate file is cached once; embedded text is downloaded again with every page |
| Image that changes often | No | Every change invalidates the whole HTML or CSS file that contains it |
The caching point matters most. A normal image file can be stored by the browser and reused on every page. An embedded one is part of the document, so it is fetched again whenever the document is. If you do embed an image, shrink it first with Compress Image so the text stays short.
Frequently asked questions
How do I convert a Base64 string to an image?
Paste the string into the Base64 decoder. It detects the image type from the decoded bytes, shows a preview and lets you download the file with the right extension, such as .png or .jpg. A data:image prefix is removed automatically.
Do I need to remove the data:image/png;base64, part first?
No. With Input format on Auto, the decoder recognises a data URI, strips the prefix and decodes the rest. Pasting the whole data URI is the easiest way.
How do I convert an image to Base64?
Drop the image into the Base64 encoder and press Encode. Turn on Wrap as a data URI if you need the data:image/...;base64, prefix for HTML or CSS. Free works with files up to 25 MB, one at a time.
Why does my Base64 string say it is truncated or invalid?
Usually part of the string was lost or an extra character crept in when it was copied. The error shows the exact position, line and column of the first bad character, and a truncated string means the end is missing, so copy the full value again.
Does the decoder handle URL-safe Base64 with - and _?
Yes. It recognises the URL-safe alphabet automatically, padded or unpadded. Whitespace and line breaks are ignored in the default mode.
Is embedding an image as Base64 good for page speed?
Only for tiny images. Base64 adds about a third to the size and the image cannot be cached separately, so photos and shared images are better served as normal files.