How to convert an image to Base64

Base64 turns a picture into text — a long string of letters, numbers and symbols that carries the image's bytes. Here is how to convert an image to Base64, what a data URI is, and when embedding an image as text is a good idea.

What Base64 is

64 characters carry any bytes as text

Base64 is an encoding, not a format. It takes any bytes — an image, a PDF, a sound — and represents them using 64 characters: A–Z, a–z, 0–9, plus and slash. The result is plain text, which means it can travel through any place text can go: an HTML file, a JSON message, an email, a database field.

The cost: about 33% larger

The cost is size: Base64 text is about 33% larger than the original file, because every three bytes become four characters. A 100 KB image becomes about 133 KB of text. That is the trade-off you accept every time you encode an image — size for portability. It also means Base64 is a bad way to store large images; its strength is small ones, where the overhead hardly matters.

Data URIs: images as text

A data URI wraps Base64 in a prefix that tells the browser what it is: data:image/png;base64,iVBORw0KGgo…. Pasted into an <img src> or a CSS background, it shows the image without any separate file — the page is self-contained. That is why data URIs appear in single-file HTML demos, email templates and icon systems.

The prefix matters: it must name the right format. A PNG encoded as data:image/jpeg will not display, and a browser gives no useful error — just a broken image. The converter on this site builds the prefix from the file you upload, so it is always correct; if you encode by hand, make sure the MIME type matches the image.

When to embed, when to link

Embed under about 10 KB, when self-contained

Embed small images as Base64 when the page must be self-contained: a single-file HTML report, an email that must display without external requests, a tiny icon repeated on every page. Under about 10 KB, embedding usually wins — it saves a network request, and the 33% overhead is a few kilobytes at most.

Link large images: cacheable and faster

Link large images as files. A 500 KB photo embedded as Base64 becomes 670 KB of text inside your HTML — the page downloads slower, the browser cannot cache the image separately, and every page load re-downloads it. The rule of thumb: if the image is bigger than a few kilobytes and appears on more than one page, keep it as a file and link it.

How to convert an image to Base64

  1. In your browser — fastest. Use the Image to Base64 Converter: drop the image in and copy the data URI or the raw Base64 string. Nothing is uploaded, and you can download the text as a .txt file.
  2. From the command line. On a Mac or Linux, run base64 -i image.png -o image.txt. On Windows, run certutil -encode image.png image.txt — certutil adds header and footer lines you need to delete, and its output is wrapped, so strip the line breaks if you need a single string.
  3. In Python. Three lines do it: import base64, read the file as bytes, then base64.b64encode(data).decode(). Add the data:image/png;base64, prefix yourself if you need a data URI.
  4. Paste it where it belongs. For HTML, put the data URI in <img src="...">; for CSS, in url("..."); for JSON, as a string value. Test the page after pasting — a truncated string shows as a broken image with no error message.

Size limits and caching

Data URIs have practical limits. Some email clients cap how large a data URI can be; very long strings can make an HTML file unwieldy to edit, and some older tools truncate lines. As a rough guide, keep embedded images under 100 KB, and prefer small icons and logos — the things that benefit most from being self-contained.

Remember too that an embedded image is downloaded with every page load. A linked image is cached by the browser after the first visit; an embedded one is part of the page, so it is fetched again every time. For a logo on every page of a site, linking is almost always the better choice — embed it only when the page must stand alone.

Related guides

Questions people ask

What is the difference between a data URI and raw Base64?

Raw Base64 is just the encoded text. A data URI adds a prefix — data:image/png;base64, — that tells the browser what the text is, so it can be used directly as an image source.

Why is the Base64 string bigger than the image?

Base64 represents every 3 bytes as 4 characters, so the text is about 33% larger than the original file. That is the price of turning binary data into plain text.

Can I convert Base64 back to an image?

Yes — the process is reversible. Decode the string (many online decoders do it) and save the result with the right extension. The converter here is one-way, image to Base64.