Base64 Image Encoder
Encode images to Base64 data URIs for HTML and CSS
Drop an image here or click to upload
PNG, JPG, SVG, WebP, GIF, AVIF supported
What Is Base64 Image Encoding?
Base64 image encoding converts binary image data into an ASCII string that can be embedded directly in HTML, CSS, or JSON without referencing an external file. The result is a data URI — a self-contained string that begins with data:image/png;base64, followed by the Base64-encoded bytes of the image. Browsers treat data URIs exactly like external image URLs: they decode the Base64 payload and render the image inline.
The technique is defined by RFC 2397 (data URI scheme) and RFC 4648 (Base64 encoding). When an image is Base64-encoded, every 3 bytes of binary pixel data become 4 ASCII characters, increasing the payload size by approximately 33%. Despite this overhead, inline images eliminate an HTTP request per asset — a meaningful optimization when the images are small (icons, logos, 1x1 tracking pixels) and the round-trip latency of a separate request would exceed the cost of the extra bytes.
Data URIs are supported in all modern browsers, email clients (with caveats), and any context that accepts standard URI syntax. They are widely used in single-page applications, HTML email templates, SVG sprites, and CSS background-image declarations where reducing the total number of network requests improves perceived load time.
Why Use This Base64 Image Encoder?
This tool converts any image to a ready-to-use data URI directly in your browser — no upload, no server, no account required.
Base64 Image Encoding Use Cases
Data URI Structure
A data URI encodes the image format and binary content in a single string. Understanding its structure helps when constructing or parsing data URIs programmatically:
| Component | Description |
|---|---|
| data: | URI scheme identifier |
| image/png | MIME type of the image |
| ;base64, | Encoding declaration |
| iVBORw0KGgo... | Base64-encoded binary data |
Image Format Comparison for Base64 Encoding
The choice of image format affects the Base64 output size, browser support, and rendering characteristics. Use the format that best matches your content type:
| Format | MIME Type | Transparency | Best For |
|---|---|---|---|
| PNG | image/png | Yes | Screenshots, icons, UI elements |
| JPEG | image/jpeg | No | Photos, gradients, large images |
| SVG | image/svg+xml | Yes | Logos, icons, scalable graphics |
| WebP | image/webp | Yes | Modern web images, smaller files |
| GIF | image/gif | Yes (1-bit) | Simple animations, small icons |
| ICO | image/x-icon | Yes | Favicons |
Code Examples
How to convert images to Base64 data URIs in popular languages and environments:
// Convert a File object to a Base64 data URI
function fileToDataUri(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(file)
// reader.result → "data:image/png;base64,iVBORw0KGgo..."
})
}
// Convert a canvas element to a data URI
const dataUri = canvas.toDataURL('image/png')
// → "data:image/png;base64,iVBORw0KGgo..."
// Convert with quality parameter (JPEG/WebP only)
const jpegUri = canvas.toDataURL('image/jpeg', 0.8)
// → "data:image/jpeg;base64,/9j/4AAQ..."import base64
# Read an image file and encode it to a data URI
with open('logo.png', 'rb') as f:
encoded = base64.b64encode(f.read()).decode('ascii')
data_uri = f'data:image/png;base64,{encoded}'
# → "data:image/png;base64,iVBORw0KGgo..."
# Detect MIME type automatically
import mimetypes
mime, _ = mimetypes.guess_type('photo.jpg')
# mime → "image/jpeg"
with open('photo.jpg', 'rb') as f:
b64 = base64.b64encode(f.read()).decode('ascii')
data_uri = f'data:{mime};base64,{b64}'import { readFileSync } from 'fs'
import { lookup } from 'mime-types'
// Read file and encode to data URI
const buffer = readFileSync('icon.png')
const base64 = buffer.toString('base64')
const mime = lookup('icon.png') // → "image/png"
const dataUri = `data:${mime};base64,${base64}`
// → "data:image/png;base64,iVBORw0KGgo..."
// From a Buffer received via HTTP/API
function bufferToDataUri(buf, mimeType) {
return `data:${mimeType};base64,${buf.toString('base64')}`
}# Encode an image to Base64 (macOS) base64 -i logo.png | tr -d '\n' # Encode an image to a full data URI (Linux) echo -n "data:image/png;base64,$(base64 -w 0 logo.png)" # Encode and copy to clipboard (macOS) echo -n "data:image/png;base64,$(base64 -i logo.png | tr -d '\n')" | pbcopy # Detect MIME type and encode (Linux, requires file command) MIME=$(file -b --mime-type image.webp) echo -n "data:$MIME;base64,$(base64 -w 0 image.webp)"