Image to Base64

Drop, pick or paste an image and copy it as raw Base64, a data URI, an <img> tag or a CSS url().

No file yet

Drop an image here, click to choose one, or paste it with Ctrl+V.PNG, JPEG, GIF, WebP, SVG, BMP, ICO or AVIF, up to 10 MB.

How to use

  1. Drop an image on the box, click it to choose a file, or copy an image (a screenshot, say) and press Ctrl+V anywhere on the page.
  2. The four outputs fill in at once: the data URI, the raw Base64, an <img> tag and a CSS background-image declaration. The line above the preview shows the format, pixel size, and how much larger the Base64 is than the file.
  3. Press Copy next to the one you need and paste it into your HTML, CSS, JSON or code.
  4. Check it in Base64 to Image → opens the decoder with the result loaded, to confirm it round-trips.

What inlining an image does

Base64 turns the bytes of an image into text that can sit inside another text file: an HTML page, a stylesheet, a JSON body, an email, a Markdown file. The result is a data URI, data:image/png;base64,…, which a browser treats like a URL that already contains the file. No second request, no path to break, no separate asset to deploy. The cost is size and cacheability, which is why it suits icons and small graphics, not photographs.

This tool reads the file in your browser, works out what it is from its first bytes, and gives you the four forms you might paste: the raw Base64 for a database column or API field, the data URI for an src or a url(), a ready <img> tag with width and height filled in so the page does not jump while it loads, and a CSS background-image declaration. It does not compress, resize or convert the image. If the file is too large to inline sensibly, that is the moment to shrink it in an image editor first, not after encoding.

Example

Load sample encodes a 157 B, 32 × 32 pixel PNG (a blue square with a white dot). The status line reports:

PNG · 32 × 32 px · 157 B → 212 characters (+35%)

The Data URI box contains:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAZElEQVR42mNgQAOqya//0xIz4AK0thivQ+htOYYjBtQBA2U53BGjDhiSDsAFaO4AYgFNHEAqoKoDyAXDwwGUglEHjDpg6DtgtBwYFEXxoKiMBkV1PNoiGp4OGO0ZDYrO6UB2zwEFpbp3WQ7rpgAAAABJRU5ErkJggg==

and the HTML <img> box:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAZElEQVR42mNgQAOqya//0xIz4AK0thivQ+htOYYjBtQBA2U53BGjDhiSDsAFaO4AYgFNHEAqoKoDyAXDwwGUglEHjDpg6DtgtBwYFEXxoKiMBkV1PNoiGp4OGO0ZDYrO6UB2zwEFpbp3WQ7rpgAAAABJRU5ErkJggg==" alt="" width="32" height="32">

The Raw Base64 box is the same string without the data:image/png;base64, prefix, and the CSS box wraps the data URI in background-image: url("…");.

Options explained

Edge cases and common errors

Do the same thing in code

JavaScript

// Browser: from a File or Blob
const dataUri = await new Promise((resolve) => {
  const r = new FileReader();
  r.onload = () => resolve(r.result);
  r.readAsDataURL(file);
});

// Node.js
import { readFile } from "node:fs/promises";
const base64 = (await readFile("logo.png")).toString("base64");
const uri = `data:image/png;base64,${base64}`;

Python

import base64
with open("logo.png", "rb") as f:
    b64 = base64.b64encode(f.read()).decode("ascii")
uri = f"data:image/png;base64,{b64}"

Shell

base64 -w0 logo.png            # Linux (GNU coreutils): one line, no wrapping
base64 -i logo.png             # macOS
[Convert]::ToBase64String([IO.File]::ReadAllBytes("logo.png"))   # PowerShell

The shell versions print raw Base64; add the data:image/png;base64, prefix yourself, using the real type of the file. To check any result, paste it into Base64 to Image. If the icon is going into a stylesheet next to a colour that has to match, the color converter gives the HEX, RGB and HSL forms of it.

Image to Base64 FAQ

Should I embed images as Base64?

Only small ones. A data URI is about a third larger than the file, cannot be cached separately from the page, and is parsed every time the page loads. It pays off for icons and tiny images that would otherwise cost an extra HTTP request, and in places that cannot reference a separate file: a single-file HTML report, an email signature, a JSON payload, a CSS file that must be self-contained. For a photo, link to the file.

Why is the Base64 bigger than the image?

Base64 represents every 3 bytes as 4 characters, so the encoded text is 4/3 the size of the file plus up to two = padding characters. The status line shows the exact figures for your image: the sample grows from 157 bytes to 212 characters. Gzip or Brotli on the server claws some of that back but never all of it.

Why does my data URI show a broken image?

The most common causes: the MIME type is wrong (image/png in front of JPEG bytes), the Base64 uses the URL-safe alphabet (- and _), which browsers do not accept inside a data URI, or the string was cut off. This tool reads the type from the file bytes and always emits the standard alphabet with padding, so its output avoids the first two. Paste the result into Base64 to Image to check the third.

Which MIME type does the tool use?

The one the bytes call for, not the one the file name claims. A .png that is really a JPEG is encoded as image/jpeg; an SVG is image/svg+xml, an ICO is image/x-icon. The tool recognises PNG, JPEG, GIF, WebP, SVG, BMP, ICO and AVIF. Anything else is refused rather than encoded with a guess.

Is my image uploaded?

No. The file is read into memory with the browser's File API (arrayBuffer()), encoded with the built-in btoa() function, and shown in the boxes. This page is a static file with no server; there is nowhere for the bytes to go. Reload the page and everything is gone.

Runs entirely in your browser: this page is a static file with no server behind it, so nothing you paste can be uploaded. Last reviewed by Parthesh Gupta. Implements RFC 2397 (data: URLs).