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.

Instant Conversion
Drag and drop an image or select a file, and the full data URI appears immediately. No waiting for server processing — the FileReader API handles everything locally.
🔒
Privacy-First Processing
Your images never leave your device. The encoding runs entirely in the browser using the native FileReader.readAsDataURL() method. No data is transmitted to any server.
🌐
All Major Formats Supported
Accepts PNG, JPEG, SVG, WebP, GIF, ICO, BMP, and any other image format your browser supports. The MIME type is detected automatically from the file.
📦
Copy-Ready Output
The output is a complete data URI string — paste it directly into an HTML img src, a CSS url() declaration, or a JSON payload without any manual formatting.

Base64 Image Encoding Use Cases

Frontend Development
Embed small icons and UI assets directly in HTML or CSS to eliminate HTTP requests. This reduces waterfall latency for critical above-the-fold images, improving Largest Contentful Paint (LCP) scores.
HTML Email Templates
Many email clients block external images by default. Inline Base64 images render immediately without requiring the recipient to click "load images," improving open rates and visual consistency.
Single-Page Applications
Bundle small assets as data URIs in your JavaScript or CSS to reduce the total number of network requests. Webpack and Vite both support automatic Base64 inlining for assets below a configurable size threshold.
API and JSON Payloads
Encode user avatars, signatures, or thumbnails as Base64 strings to include them directly in JSON API responses. This avoids the need for a separate file-hosting endpoint for small images.
Documentation and Markdown
Embed diagrams or screenshots as data URIs in Markdown files or README documents. The images travel with the document and display correctly without external hosting.
Testing and Prototyping
Use Base64 data URIs as placeholder images in test fixtures, Storybook stories, or prototype HTML files. No need to set up a static file server or manage image assets during early development.

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:

ComponentDescription
data:URI scheme identifier
image/pngMIME 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:

FormatMIME TypeTransparencyBest For
PNGimage/pngYesScreenshots, icons, UI elements
JPEGimage/jpegNoPhotos, gradients, large images
SVGimage/svg+xmlYesLogos, icons, scalable graphics
WebPimage/webpYesModern web images, smaller files
GIFimage/gifYes (1-bit)Simple animations, small icons
ICOimage/x-iconYesFavicons

Code Examples

How to convert images to Base64 data URIs in popular languages and environments:

JavaScript (browser)
// 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..."
Python
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}'
Node.js
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')}`
}
CLI (bash)
# 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)"

Frequently Asked Questions

What is the maximum image size for Base64 encoding?
There is no hard limit in the Base64 specification itself. Browser data URI support varies: most modern browsers handle data URIs up to several megabytes. However, Base64 encoding increases size by 33%, so a 1 MB image becomes approximately 1.33 MB of text. For images larger than 10-20 KB, serving them as separate files with proper caching is more efficient.
Does Base64 encoding reduce image quality?
No. Base64 is a lossless encoding — it converts binary data to text without altering the content. The decoded data is byte-for-byte identical to the original file. Any quality loss would come from the image format itself (e.g., JPEG compression), not from Base64 encoding.
Why is Base64-encoded output larger than the original image?
Base64 represents 3 bytes of binary data using 4 ASCII characters, resulting in a 33% size increase. A data URI also adds a prefix (e.g., data:image/png;base64,) of roughly 25-30 characters. For small images (under 2-5 KB), this overhead is offset by eliminating an HTTP request. For larger images, the size penalty makes external files more efficient.
Can I use Base64 images in CSS background-image?
Yes. Use the data URI as the value of the url() function: background-image: url('data:image/png;base64,...'). This works in all modern browsers. It is most effective for small, frequently used background patterns or icons where avoiding an HTTP request is worth the slightly larger CSS file.
Is Base64 image encoding safe for sensitive images?
Base64 is an encoding, not encryption. Anyone who sees the data URI can decode it back to the original image. Do not rely on Base64 to protect confidential images. This tool processes images entirely in your browser — no data is sent to any server — but the resulting data URI is plain text that can be read by anyone with access to your HTML or CSS source.
How do I convert a Base64 data URI back to an image file?
In JavaScript, use fetch() with the data URI and call blob() on the response, then create a download link. In Python, split the string at the comma, decode the Base64 portion with base64.b64decode(), and write the bytes to a file. You can also use the Base64 Image Decoder tool on this site.
Should I Base64-encode all images on my website?
No. Base64 inlining is beneficial for small images (icons, logos, 1x1 pixels) where the HTTP request overhead exceeds the 33% size penalty. For larger images, use separate files with proper cache headers — the browser can cache them independently and load them in parallel. A common threshold is 2-8 KB: images below this size benefit from inlining, while larger images should be served as external files.