← Back to Blog
Developer Tools9 min readJan 2026

How to Convert Images to Base64: Complete Guide for Developers

Embedding an image directly in HTML, CSS, or an API payload sounds clever — until you remember the file size penalty. This guide explains how Base64 image encoding works, when it's the right call, and how to do it in every major language.

What Is Base64 Encoding?

Base64 is a way to represent binary data as ASCII text. It uses 64 safe characters — A-Z, a-z, 0-9, plus + and / — to ensure the data passes safely through systems that can't handle raw binary, like email or URL parameters.

Three bytes of binary input become four characters of Base64 output. The result is always larger than the input, but it's always text-safe.

Why Convert Images to Base64?

When TO Use Base64 Images

When NOT to Use Base64

How to Convert Using ConvertDox

The ConvertDox Image to Base64 tool handles the conversion entirely in your browser — no upload, no privacy compromise:

  1. Drop your image (PNG, JPG, GIF, SVG, WebP) onto the upload area.
  2. The Base64 string and full data URI appear instantly.
  3. Click Copy as Data URI to paste straight into HTML or CSS.

Because the file never leaves your machine, this is safe to use with confidential or work-related assets.

Using Base64 in HTML

An <img> tag accepts a data URI as the src attribute. The format is data:[mime-type];base64,[data].

<img
  src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="
  alt="1x1 transparent pixel"
  width="20"
  height="20"
/>

Using Base64 in CSS

Same idea, used as a CSS background-image:

.icon-check {
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciLi4uLjwvc3ZnPg==");
  background-repeat: no-repeat;
  width: 16px;
  height: 16px;
}

This is the technique most CSS-in-JS libraries use for inline SVG icons.

Base64 in JavaScript

Browsers expose btoa() to encode and atob() to decode. For files, use the FileReader API:

function fileToBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.onload = () => resolve(reader.result)
    reader.onerror = reject
    reader.readAsDataURL(file)
  })
}

// usage
const input = document.querySelector('input[type=file]')
input.addEventListener('change', async e => {
  const dataUri = await fileToBase64(e.target.files[0])
  console.log(dataUri) // "data:image/png;base64,iVBORw0KG..."
})

Base64 in Python

Python's standard library has built-in Base64 support:

import base64

# Encode an image file
with open("logo.png", "rb") as f:
    encoded = base64.b64encode(f.read()).decode("ascii")
    data_uri = f"data:image/png;base64,{encoded}"

# Decode back to bytes
raw = base64.b64decode(encoded)
with open("logo-copy.png", "wb") as f:
    f.write(raw)

Performance Considerations

Frequently Asked Questions

Does Base64 reduce image file size?

No — the opposite. Base64 encoding adds roughly 33% to the size of the original binary because three bytes become four ASCII characters. Only use it when the convenience outweighs the size penalty.

Can browsers cache Base64 images?

They cache the HTML or CSS file that contains the data URI, but the image itself can't be cached separately. That means a Base64 image is re-downloaded every time the surrounding file changes, even if the image is unchanged.

Is Base64 the same as encryption?

No. Base64 is encoding — fully reversible, no key, no secret. Anyone can decode it. Never use Base64 to hide passwords or sensitive data.

What image formats can be Base64-encoded?

All of them. PNG, JPG, GIF, SVG, WebP, AVIF — Base64 is byte-level, so it doesn't care about the format. The MIME type in the data URI tells the browser how to render the decoded bytes.

How big is too big for a Base64 image?

A practical ceiling is around 5–10 KB. Past that, the size penalty, blocked caching, and slower HTML parsing outweigh the benefit of removing a single HTTP request. Use real image files for anything larger.

🖼️

Convert an Image to Base64

Drag, drop, copy the data URI — all locally, never uploaded.

Open Image to Base64 →

Related Articles

Developer Tools
What Is a JSON Formatter and Why Every Developer Needs One
Security
10 Best Free Password Generators in 2026