How to Decode a Base64 String or Data URI Into an Image File

Turn a Base64 string or data URI from an API, log or database back into a viewable image, identify its real format, and fix common decoding errors.

Updated September 29, 2026 5 min read

Use the free Base64 to ImageDecode Base64 or data URIs back into image files

You are debugging an API response and one field is a wall of characters starting with iVBORw0KGgo. A support ticket includes a screenshot pasted as a data URI. A database column called signature_image holds text instead of a file path. In every case there is a picture hiding in that text, and the fastest way to understand the problem is to see it.

This guide shows how to recognise Base64 image data, how to decode it into a real file in a browser or terminal, how to tell what format the image really is, and what the usual error messages mean when decoding fails.

Recognising image data in Base64

Base64 text uses only letters, digits, +, / and trailing = signs (or - and _ in the URL-safe variant). Image data often announces itself in two ways.

A data URI header. If the string starts with data:image/...;base64,, the part before the comma tells you the claimed type and the part after is the payload.

A telltale start. Every image format begins with fixed "magic bytes", and those bytes encode to predictable Base64 prefixes:

Base64 starts with Real format Magic bytes
iVBORw0KGgo PNG 89 50 4E 47 0D 0A 1A 0A
/9j/ JPEG FF D8 FF
R0lGOD GIF GIF87a or GIF89a
UklGR WebP (RIFF container) RIFF....WEBP
Qk BMP BM
PHN2Zy or PD94bW SVG (text starting <svg or <?xml) -
JVBERi0 PDF, not an image %PDF-

Memorising the first three covers most cases you will meet in practice.

Why the declared type can be wrong

The MIME type in a data URI is only a label written by whatever created it. It is common to find data:image/png;base64,/9j/..., which is really a JPEG, because a developer hard-coded the prefix. Browsers are forgiving and display it anyway, but saving it as .png produces a file with the wrong extension, and strict image libraries may refuse it. The reliable approach is to decode the bytes and check their magic numbers.

Decode Base64 with our tool

Our Base64 to Image decoder does the detection for you and runs entirely in your browser, which matters when the data came from a production system.

  1. Open Base64 to Image and paste the text into Base64 string or data URI. You can paste either the full data: URI or just the raw Base64. Line breaks, spaces and URL-safe characters (- and _) are handled automatically. Click Load sample if you want to see how it works first.
  2. The image appears in the preview straight away. The panel beside it shows Detected (the real format read from the bytes), the MIME type, the File size and the Dimensions.
  3. If the data URI claimed a different type from what the bytes contain, a note tells you, and the download uses the real type.
  4. Type a File name. The correct extension is added for you.
  5. Click Download (the button shows the detected format, such as Download PNG).

The decoder recognises PNG, JPEG, GIF, WebP, SVG, BMP, ICO, TIFF, AVIF and HEIC. If your browser cannot display a format such as TIFF or HEIC, you can still download the file and open it elsewhere. If the data turns out to be a PDF, it tells you so.

Other ways to decode Base64

Terminal

Save the Base64 text (without any data: prefix) to a file, then:

base64 --decode input.txt > output.png      # Linux and recent macOS

In PowerShell:

$b = [Convert]::FromBase64String((Get-Content input.txt -Raw))
[IO.File]::WriteAllBytes("output.png", $b)

You must choose the extension yourself. On Linux and macOS, run file output.png afterwards to confirm the real format.

The browser address bar

Pasting a complete data URI into the address bar of some browsers displays the image, but many browsers now block navigating to data URLs from the address bar for security reasons. It is not a dependable method.

In code

Buffer.from(str, "base64") in Node.js and base64.b64decode(s) in Python return the raw bytes, which you then write to disk. This is the right approach inside an application or test suite.

Troubleshooting decoding errors

"Contains characters outside A-Z, a-z, 0-9, +, / and ="

Something that is not Base64 slipped in: surrounding quotes from JSON, a backslash from escaped strings (\/), or the data: prefix pasted twice. Copy only the value, not the surrounding syntax. If the data is escaped JSON, unescape it with the String Escape tool first.

"The Base64 text is incomplete"

The length is wrong, which almost always means the string was cut off. Logs, spreadsheet cells and chat apps often truncate long values. Get the full value from the source, for example with the API tool's "copy response" feature rather than selecting text on screen.

"Not a recognised image format"

The text decoded correctly but the bytes are not an image. It may be encrypted, compressed (gzip data starts with H4sI in Base64), or a different kind of file. For plain text or tokens, try the general Base64 Converter. If it looks like a token with dots between sections, it may be a JWT; inspect it with the JWT Decoder.

The image appears but is partly grey or corrupted

The data was truncated partway through, but still ended on a valid boundary. JPEGs in particular render the top portion and leave the rest blank. Again, retrieve the complete value.

A percent-encoded SVG

Data URIs without ;base64 contain URL-encoded text, which is common for SVG (data:image/svg+xml,%3Csvg...). The tool decodes these as well.

Going the other direction

If you need to create a data URI from a file, for example to test how an API handles a known image, use Image to Base64. To reduce the size of a decoded image before storing it again, run it through the Image Compressor.

Frequently asked questions

How can I tell what format a Base64 image is?

Look at the first characters: iVBORw0KGgo is PNG, /9j/ is JPEG and R0lGOD is GIF. A decoder that reads the magic bytes, like ours, reports the real format automatically.

Do I need to remove the data:image prefix before decoding?

Not with our tool, which accepts either form. Command-line decoders need the raw Base64 only, so remove everything up to and including the comma.

Why does my decoded image not open?

Usually the string was truncated, contains stray characters such as quotes or escaped slashes, or the file was saved with the wrong extension. Check that you copied the complete value.

Is it safe to decode Base64 from an unknown source?

Decoding itself does not run anything, and our tool works locally without uploading the data. Treat the resulting file as you would any downloaded file, and be careful with SVG, which can contain scripts when opened directly.

What is URL-safe Base64?

It is a variant that uses - and _ instead of + and / so the text can sit in URLs without escaping. It decodes to exactly the same bytes.

Try it now: Base64 to Image

Loading tool…