Image to Base64

Drop an image to get a Base64 data URI, plus ready-to-paste HTML and CSS. Your file never leaves your browser.

This tool runs in your browser. We never send what you type or paste to a server.

When to inline images

Inlining a small icon as a data URI saves an HTTP request, which can help for tiny images used on every page. For anything larger than a few kilobytes it usually hurts: Base64 adds about 33% to the size, and inlined images can't be cached separately from the page or stylesheet.

For SVGs, a URL-encoded data URI is often smaller than Base64; for icons, consider an inline <svg> instead.

Example

A data URI puts the image inside your HTML or CSS, saving a separate request:

<img src="data:image/png;base64,iVBORw0KGgo…" alt="">

The same string works in CSS as background-image: url("data:image/png;base64,…"); the tool gives you both forms ready to paste.

Anatomy of a data URI

A data URI (RFC 2397) has three parts: the scheme data:, a media type, and the content after a comma, here marked ;base64 because it is Base64-encoded:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=
data:        the scheme
image/png    the media type
;base64      how the content is encoded
,iVBOR…      the file's bytes in Base64

That one is a complete 1 × 1 pixel PNG: 68 bytes of file, 114 characters of URI. The tool takes the media type from what your browser reports for the file, and the status line shows how much bigger the URI is than the file. Every 3 bytes become 4 characters, so expect about a third more, plus the prefix. Gzip or Brotli on the page wins some of that back, but not all, and the image now downloads again with every page or stylesheet that contains it.

The same URI works in an <img src>, a CSS url(), a <link rel="icon"> favicon, or a JSON payload. If your site sends a Content Security Policy, its img-src must include data:, or the browser refuses to show these images.

SVG: skip the Base64

SVG is text, so it can go into a data URI without Base64, which is usually shorter and stays readable. Escaping it fully with encodeURIComponent is safe but can come out longer than Base64. Escaping only the characters that cause trouble (%, #, <, >) and switching double quotes to single gives the shortest result, which works inside a double-quoted CSS url("…"):

const svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2 2"><circle cx="1" cy="1" r="1" fill="#e33"/></svg>';
const asBase64 = `data:image/svg+xml;base64,${Buffer.from(svg).toString('base64')}`;
const asText = `data:image/svg+xml,${encodeURIComponent(svg)}`;
const minimal = `data:image/svg+xml,${svg.replace(/"/g, "'").replace(/[%#<>]/g, encodeURIComponent)}`;
console.log(asBase64.length, asText.length, minimal.length);   // → 166 206 138
console.log(minimal);   // → data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2 2'%3E%3Ccircle cx='1' cy='1' r='1' fill='%23e33'/%3E%3C/svg%3E

The # matters most: unescaped, it starts a URL fragment, so a colour like fill="#e33" cuts the image off at that point.

Making data URIs in code and on the command line

For a build step or a script, read the file as bytes and encode it. In Node:

const bytes = readFileSync('dot.png');
const uri = `data:image/png;base64,${bytes.toString('base64')}`;
console.log(bytes.length, uri.length);   // → 68 114   bytes in, characters out
console.log(uri.slice(0, 40));           // → data:image/png;base64,iVBORw0KGgoAAAANSU

In Python, mimetypes guesses the type from the file name:

def data_uri(path):
    mime = mimetypes.guess_type(path)[0] or 'application/octet-stream'
    with open(path, 'rb') as f:
        return f'data:{mime};base64,{base64.b64encode(f.read()).decode("ascii")}'

print(data_uri('dot.png')[:40])     # → data:image/png;base64,iVBORw0KGgoAAAANSU

In a shell, remember -w 0: GNU base64 wraps lines at 76 characters by default, and a line break inside a data URI breaks it (macOS base64 does not wrap unless told to). In PowerShell, .NET file methods resolve relative paths against the process directory rather than your current location, so pass a full path:

uri="data:image/png;base64,$(base64 -w 0 dot.png)"     # -w 0: one line, no wrapping
printf '%s\n' "${uri:0:40}"                           # → data:image/png;base64,iVBORw0KGgoAAAANSU
printf '%s' "${uri#*,}" | base64 -d > copy.png        # and back again
cmp dot.png copy.png && echo same                     # → same
$b64 = [Convert]::ToBase64String([IO.File]::ReadAllBytes("$PWD\dot.png"))   # .NET needs a full path
"data:image/png;base64,$b64".Substring(0, 40)          # → data:image/png;base64,iVBORw0KGgoAAAANSU

In the browser, FileReader.readAsDataURL(file) does the whole job for a file from an input or drop, which is what this tool uses, and canvas.toDataURL('image/webp') gives you an image you have drawn or resized.

Turning a data URI back into a file

Strip everything up to and including the first comma and Base64-decode the rest. The part before the comma tells you the file type. The Base64 decoder does this in the browser and offers the result as a download; in code:

const m = uri.match(/^data:([^;,]+);base64,(.*)$/);
writeFileSync('copy.png', Buffer.from(m[2], 'base64'));
console.log(m[1], readFileSync('copy.png').equals(bytes));   // → image/png true

Questions

Is there a size limit?

Files up to 10 MB work, but data URIs that large are rarely a good idea in production.

How do I turn Base64 back into an image?

Paste the full data URI (starting data:image/…) into your browser's address bar, or decode it with our Base64 tool.

Is the image uploaded?

No. The file is read by your browser and converted on your device.

How do I convert an image to Base64 in JavaScript?

In the browser, pass the File to FileReader and call readAsDataURL; the result is a complete data URI. In Node, read the file with fs.readFileSync(path).toString('base64') and add the data:image/png;base64, prefix yourself.

Why does my data URI image not show?

Common causes are a line break inside the Base64 (from a wrapping encoder), the wrong media type, an unescaped # in an SVG data URI, or a Content Security Policy whose img-src does not allow data:.

Should I use Base64 for SVG images?

Usually not. SVG is text, so a URL-encoded data URI is often shorter and still readable, as long as you escape #, %, < and >. For icons on a web page, inline <svg> markup is often better still.