How to Decode a Base64 String Back Into an Image File

A long Base64 string being turned back into a picture file

You have a block of text that starts with something like iVBORw0KGgoAAAANSUhEUg… or data:image/jpeg;base64,/9j/4AAQ… — found in an API response, a database column, a CSS file or an exported document — and you want the actual picture. Base64 is just an encoding, not encryption or compression, so turning it back into a file is easy and loses nothing. This guide shows four ways to do it without installing anything, and how to work out what kind of image you have.

If you are going the other direction — image to text — start with the beginner guide to converting an image to Base64.

Need the text instead of the picture?

Convert any PNG, JPG, WebP, GIF or SVG to a Base64 data URI in one click. Everything happens in your browser — the file is never uploaded.

Open the Image to Base64 Converter →

Step zero: work out what you have

A Base64 string comes in two shapes. A data URI has a header: data:image/png;base64,iVBOR…. Raw Base64 is only the characters after the comma. Most decoding methods want the raw part, so remove everything up to and including the comma if it is there.

Then identify the format. Every image file begins with a fixed signature, and Base64 spells that signature the same way every time, so the first characters give it away:

Starts withFormatSave as
iVBORw0KGgoPNG.png
/9j/JPEG.jpg
R0lGODGIF.gif
UklGRWebP.webp
PHN2Zy or PD94bWwSVG.svg
AAABAAICO (icon).ico

If the string came with a data URI header, the MIME type in it (image/png, image/jpeg) tells you directly.

Method 1: Your browser (no tools, any computer)

  1. Make sure you have the full data URI. If you only have the raw text, add the header yourself: data:image/png;base64, followed by the string (use image/jpeg for strings that start with /9j/).
  2. Paste the whole thing into your browser's address bar and press Enter. Very long strings can be slow to paste but generally work in Chrome, Edge and Firefox; Safari can be fussier.
  3. The image opens in its own tab. Right-click it and choose Save image as….

If the tab stays blank or shows an error, the string is damaged or truncated — go back to the source and copy it again. Some browsers refuse to navigate to data URIs for HTML, but images are usually fine.

Method 2: Windows PowerShell

PowerShell is built into every Windows PC. Open the Start menu, type PowerShell and open it, then paste these two lines, replacing the text in quotes with your raw Base64 (no prefix):

$b64 = "iVBORw0KGgoAAAANSUhEUg..."
[IO.File]::WriteAllBytes("$HOME\Desktop\decoded.png", [Convert]::FromBase64String($b64))

A file named decoded.png appears on your Desktop. Change the extension to match the format table above. For a very long string, save it in a text file first and read it with $b64 = (Get-Content "$HOME\Desktop\string.txt" -Raw).Trim() — pasting hundreds of kilobytes straight into a console can hang it.

Method 3: Mac or Linux Terminal

Save the raw Base64 text in a file called string.txt on your Desktop, open Terminal, and run:

cd ~/Desktop
base64 --decode -i string.txt -o decoded.png

On Linux the equivalent is base64 -d string.txt > decoded.png. Flags differ between versions of the tool (older Macs use a capital -D), so if one form errors, try the other. Open the result with Preview or your default viewer. If the file is empty or corrupt, check for a stray data URI prefix at the start of string.txt.

Method 4: Python (for many files or automation)

import base64, re

text = open("string.txt").read().strip()
text = re.sub(r"^data:image/[a-z+]+;base64,", "", text)   # drop prefix if present
text = "".join(text.split())                              # drop any line breaks or spaces

with open("decoded.png", "wb") as f:
    f.write(base64.b64decode(text))

Python is the right choice when you have a spreadsheet or database full of encoded images: loop over the rows, decode each one, and name the files from an ID column.

Common reasons a decoded file will not open

  • The prefix is still there. The decoder treats data:image/png;base64, as image data and produces garbage. Strip it.
  • Whitespace or line breaks. Some systems wrap Base64 every 76 characters. Strict decoders reject the line breaks; remove them.
  • URL-safe variant. Strings containing - and _ instead of + and / use the URL-safe alphabet. Swap them back before decoding.
  • Truncated string. Chat apps, spreadsheets (Excel cells cap at 32,767 characters) and some terminals cut long strings silently. Compare the length with the source.
  • Wrong extension. A JPEG saved as .png often still opens, but some programs refuse. Match the extension to the signature.
  • Padding missing. Valid Base64 length is a multiple of four; add = characters to the end until it is.

A privacy warning about online decoders

Search for “Base64 to image” and many results are websites that take your pasted text and send it to a server. If the string is a private photo, a scanned ID or a signature, you may have just uploaded it to a stranger. The four methods above all run on your own computer, which is safer. Our own Image to Base64 converter works the other way round and processes the file entirely in your browser — nothing leaves your device.

Shrink it afterwards

Images extracted from Base64 are often tiny thumbnails or oversized originals. If the decoded picture is too big to email, run it through the image compressor; if you need a different pixel size, use the image resizer. And if you plan to embed a picture again, see how to use a Base64 image in HTML and CSS.