What Is Base64 to Image Conversion
Base64 to Image is the reverse process of converting a Base64 encoded text string back to a binary image file.
A typical Base64 image string starts with a Data URI prefix:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...
^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
scheme MIME type Base64 encoded image data
Why You Need Base64 to Image Conversion
- API response handling: Backend returns Base64 image data, frontend needs to save as image
- Browser caching: Extract inline Base64 images as independent files
- Data migration: Extract Base64-stored images from databases
- Email processing: Parse Base64 attachments in emails
- Screenshot saving: Save HTML Canvas screenshot Base64 data as files
- Log analysis: Extract Base64-encoded images from logs
How to Use an Online Tool
Using DevToolkit Pro's Base64 to Image Tool:
- Paste the Base64 encoded string
- Supports with or without Data URI prefix
- Auto-detects image format (PNG, JPEG, GIF, etc.)
- Preview the restored image
- One-click download as image file
Base64 to Image in Code
JavaScript (Browser)
function base64ToImage(base64String) {
// Remove Data URI prefix
const base64 = base64String.replace(/^data:image\/\w+;base64,/, '');
// Decode to binary
const binaryString = atob(base64);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
// Create Blob and download
const blob = new Blob([bytes], { type: 'image/png' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'image.png';
a.click();
}
Node.js
const fs = require('fs');
function base64ToImage(base64String, outputPath) {
const base64 = base64String.replace(/^data:image\/\w+;base64,/, '');
const buffer = Buffer.from(base64, 'base64');
fs.writeFileSync(outputPath, buffer);
console.log(`Image saved to ${outputPath}`);
}
// Usage
base64ToImage('data:image/png;base64,iVBORw0KGgo...', 'output.png');
Python
import base64
def base64_to_image(base64_string, output_path):
# Remove Data URI prefix
if ',' in base64_string:
base64_string = base64_string.split(',')[1]
# Decode and save
image_data = base64.b64decode(base64_string)
with open(output_path, 'wb') as f:
f.write(image_data)
Common MIME Types
| MIME Type | Description | Extension |
|-----------|-------------|-----------|
| image/png | PNG image | .png |
| image/jpeg | JPEG image | .jpg / .jpeg |
| image/gif | GIF animation | .gif |
| image/webp | WebP image | .webp |
| image/svg+xml | SVG vector | .svg |
| image/bmp | BMP bitmap | .bmp |
FAQ
Does Base64 to Image Cause Quality Loss?
No. Base64 is lossless encoding — decoded data matches the original binary exactly. Quality loss comes from image compression (e.g., JPEG), not Base64 encoding.
Why Can't I Open the Converted Image?
Common causes: truncated Base64 string, illegal characters, MIME type mismatch. Check string integrity and ensure no extra spaces or line breaks.
Why Is the Image Larger?
Base64 encoding inherently increases data by ~33%. This is the encoding overhead — the original size is restored after decoding.
This article is brought to you by DevToolkit Pro. More developer tools at the homepage.
相关文章
URL Encoding Explained: How to Handle Spaces, Chinese Characters & Special Symbols
Learn how percent encoding works for spaces, Chinese characters, and special symbols. Understand %20 vs +, UTF-8 multi-byte encoding, and avoid double encoding bugs.
Online URL Encoder/Decoder: Percent-Encoding Explained
Learn URL encoding (percent-encoding) fundamentals. Understand when and why to encode URLs, special character handling, and best practices for web development.
Online Unicode Encoder/Decoder: Handle Unicode Character Conversion
Learn Unicode encoding and decoding principles. Understand differences between UTF-8, UTF-16, and Unicode escape sequences, and how to use an online tool for Unicode conversion.