Blog · How-To
Base64 to PNG: Convert Encoded Strings Back to Images
Turn a Base64 string back into a PNG image. Extract images from Data URIs, API responses, and JSON payloads. Every method, from one-click to Python script.
An API returned a Base64 string for an image. A JSON config file has a "thumbnail": "iVBORw0KGgo..." field. Or you are looking at a Data URI and need the actual PNG file, not a text string. Converting Base64 back to a PNG image is one decode operation — the trick is knowing whether your string is raw Base64 or a Data URI and whether it actually contains a PNG. Here is every method.
.png file automatically. For programmatic use: decode with base64.b64decode() in Python or atob() + Blob in JavaScript, then write the bytes to a file with a .png extension.Method 1: The Online Tool (One Click)
Open the Base64 decoder, paste your string, and click Decode. The tool reads the first few bytes of the decoded data. If it sees the PNG magic bytes (89 50 4E 47), it labels the output as PNG, shows a preview, and offers a Download button. If your string is a Data URI (data:image/png;base64,...), it strips the prefix automatically.
The tool also handles the reverse operation — converting a PNG to Base64 — if you need the encode direction instead.
Method 2: Python — Decode and Save to Disk
b64_string = "iVBORw0KGgoAAAANSUhEUg..."
# Strip Data URI prefix if present
if b64_string.startswith("data:"):
b64_string = b64_string.split(",")[1]
# Decode and write to file
image_bytes = base64.b64decode(b64_string)
with open("output.png", "wb") as f:
f.write(image_bytes)
print(f"Saved {len(image_bytes)} bytes to output.png")
The critical line is "wb" (write-binary) in the open() call. If you accidentally use "w" (text mode) on Windows, Python inserts carriage returns into your binary data and the PNG will be corrupted — it will not open in any image viewer, with no useful error message. If your decoded PNG refuses to open, check that you wrote the file in binary mode.
Method 3: JavaScript — Download from the Browser
If you are building a web app and need to trigger a PNG download from a Base64 string:
// Strip Data URI prefix if present
const raw = b64.replace(/^data:image\/\w+;base64,/, "");
const bytes = Uint8Array.from(atob(raw), c => c.charCodeAt(0));
const blob = new Blob([bytes], { type: "image/png" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url; a.download = filename; a.click();
URL.revokeObjectURL(url);
}
This works for any image format — just change the MIME type in the Blob constructor. PNG, JPEG, WebP, GIF: the decode logic is identical. Only the file extension and MIME type change. See our Base64 to file guide for a version that auto-detects the format from the byte headers.
Method 4: Command Line
echo "iVBORw0KGgoAAAANSUhEUg..." > encoded.b64
# Decode to PNG (Linux/WSL)
base64 -d encoded.b64 > output.png
# Decode to PNG (macOS)
base64 -D encoded.b64 > output.png
# One-liner with echo (Linux)
echo -n "iVBORw0KGgo..." | base64 -d > output.png
If you get a "base64: invalid input" error, check for whitespace or line breaks in your string — the base64 command is less forgiving of formatting than Python or JavaScript. A quick cleanup: tr -d '\n\r ' < encoded.b64 | base64 -d > output.png strips whitespace before decoding.
Frequently Asked Questions
How do I know if a Base64 string contains a PNG?
Decode the first few bytes and check the magic number. PNG files always start with ‰PNG (bytes: 89 50 4E 47). In Base64, PNG-encoded strings almost always start with iVBORw0KGgo. Our online decoder detects this automatically.
My decoded PNG won't open. What went wrong?
Likely one of three things: you forgot to strip the Data URI prefix before decoding, you wrote the file in text mode instead of binary mode (especially on Windows), or the Base64 string is truncated or contains whitespace. Try decoding with the online tool first — if it opens there, the problem is in your write step, not the decode.
Can I convert Base64 to JPEG or WebP the same way?
Yes. The decode process is identical regardless of image format — Base64 is format-agnostic. The only difference is the file extension you use when writing the decoded bytes, and the MIME type in the Data URI prefix. JPEG files start with /9j/ in Base64.
Convert Base64 to PNG now. One click.
Paste your string, get your image. Auto-detects format, strips Data URI prefixes.
Open the Decoder