Toolezs
Back to Blog
Tutorials

How to Convert an Image to Base64 (Free, No Upload)

Convert an image to Base64 in your browser, with JavaScript or with Python, and learn when a data URI helps and when it hurts. No upload, no sign-up.

Mark Steven

Mark Steven • Content Writer

Glossy violet picture icon beside lines of Base64 characters, centred inside concentric rings on a pale lavender dotted background.

A picture on a web page is usually a separate file the browser has to fetch. Base64 is the other option: the image itself, written as text, pasted straight into the HTML, CSS or JSON that needs it. That saves a request and keeps a picture inside a file that only accepts text. This guide shows how to convert an image to Base64 online, in JavaScript and in Python, how to decode it back to an image, and when a data URI is the wrong tool.

Quick answer: open the Image to Base64 converter, drag your image onto the page, and choose the output you need — a data URI, the raw Base64 string, an HTML img tag, a CSS background or Markdown. Copy it, or download it as a .txt file. The image never leaves your browser: the file is read with the browser's own file reader, so the string holds the exact bytes of the original.

What Base64 actually is

Base64 writes binary data using only the characters that survive everywhere: A-Z, a-z, 0-9, + and /, with = as padding. Three bytes of data become four characters of text, which is where the familiar 33% size penalty comes from. Nothing is compressed and nothing is thrown away — decode the text and you get back byte for byte what you started with.

That matters because most places in software only carry text: JSON has no way to hold a JPEG, and an HTML attribute is just a string. Base64 is the standard bridge — encode on one side, decode on the other, and the picture survives the trip.

  • Base64 is not encryption. Anyone can decode it in a second, so never use it to hide anything private.
  • A data URI is Base64 with a label. data:image/png;base64,… adds the media type the browser needs before it can render the picture.
  • The payload is the same either way. The characters after the comma in a data URI are the plain Base64 string.

How to convert an image to Base64 online

  1. Open the Image to Base64 converter.
  2. Drag your image onto the page, or click to browse. You can add up to 10 images at a time, 5 MB each.
  3. Pick the output shape: a Data URI, Base64 only, an HTML img tag, a CSS background or Markdown.
  4. Copy the string from the card, or download it as a .txt file.
  5. Use Copy all or Download all to grab several images at once.
  6. Need the picture back? Paste the string into the Base64 to Image tab, or open the decoder directly.

Everything happens on your device, so there is no upload step, no account and no queue. A large photo still encodes instantly, because the browser is copying bytes it already has.

How to convert an image to Base64 in JavaScript

In the browser there is no file system to read from. A visitor hands you a file through an input or a drag-and-drop event, and FileReader.readAsDataURL turns it into a data URI. Everything after the first comma is the Base64 payload:

convert-image.js
const file = document.querySelector('input[type="file"]').files[0];

const reader = new FileReader();
reader.onload = () => {
  const dataUri = String(reader.result);
  const base64 = dataUri.split(",")[1]; // everything after the comma
  console.log(base64);
};
reader.readAsDataURL(file);

readAsDataURL copies the file's own bytes, so nothing is re-encoded, resized or compressed on the way through. If you want the data URI rather than the payload, keep the whole reader.result string; the two are the same text with a different starting point.

How to convert an image to Base64 in Node.js

On the server the job is smaller, because fs.readFileSync already returns a buffer and Buffer.toString("base64") does the encoding:

convert-image.mjs
import { readFileSync } from "node:fs";

const base64 = readFileSync("photo.png").toString("base64");
const dataUri = `data:image/png;base64,${base64}`;

Run against the same photo, that produces the identical string the browser produces. Base64 is a format, not a platform API, which is exactly why it is used as a hand-off between programs that share nothing else. Add the type to a data: prefix when the string is going somewhere that needs it.

How to convert an image to Base64 in Python

Python's standard library does the same job with one import. Read the file in binary mode, pass the bytes to base64.b64encode, and decode the result to a string — b64encode returns bytes, which surprises people the first time they print it. To go back, base64.b64decode returns bytes again and an ordinary write in binary mode saves the picture. No third-party package is involved, which is why the pattern turns up in scripts that have to stay dependency-free.

One detail matters when comparing two strings: some encoders wrap long output at 76 characters and others do not, so strip whitespace before you check that two Base64 strings match. Whitespace is legal inside a Base64 block, which is why the decoder here accepts a string pasted with line breaks.

How to decode Base64 back to an image

Decoding is the same operation backwards, and in the browser it takes three lines: turn the text into bytes, wrap the bytes in a Blob with the right type, and let the browser show it.

decode-image.js
const bytes = Uint8Array.from(atob(base64), (char) => char.charCodeAt(0));
const blob = new Blob([bytes], { type: "image/png" });
const url = URL.createObjectURL(blob); // put this in an <img> src

Or skip the code: open the Base64 to Image tab, paste the string or the whole data URI, and the tool does the rest. It checks the text, reads the image type from the file's own signature, shows a preview with the size and dimensions, and downloads the picture under the name you choose.

When a data URI helps, and when it hurts

A data URI is a trade, not a free upgrade. It saves a request and keeps a picture inside a text file, and it pays for that with size and flexibility. Use one when:

  • The image is small — an icon, a logo, a bullet, a tiny avatar — so the 33% overhead is a few hundred bytes.
  • The file has to be self-contained: a single-file HTML demo, a note you paste into a chat, a fixture in a test.
  • Only text can travel: a JSON payload, a config file, an email template.

Avoid one when:

  • The image is a photograph or a screenshot of any size. Base64 makes a large file larger, and the browser cannot start painting until the whole string has been parsed.
  • The same picture appears on many pages. A normal image file is downloaded once and cached; a data URI is repeated in every page that carries it.
  • Caching, lazy loading or a CDN matters. None of those work on a string embedded in the document.
  • The string ends up in version control. A diff of a 20,000-character line tells a reviewer nothing.

The rule the sizes point to: Base64 for small, embedded, self-contained things; a normal image file for everything else. When the picture is too big to encode comfortably, shrink it with Compress Image or convert it with the Image Converter first.

Common mistakes to avoid

  • Pasting a data URI where a plain Base64 string is expected. Some APIs want the payload only, with no data: prefix — the tool gives you both so you can pick.
  • Cutting a long string short. A missing character at the end usually shows up as a broken image rather than a clear error, which is why the decoder here checks the text before it renders anything.
  • Trusting the prefix. A string can say image/png and hold JPEG bytes. Reading the file's own signature is the reliable way to name the type.
  • Encoding twice. A Base64 string is already text; wrapping it in another layer of Base64 only makes it bigger.
  • Assuming it hides anything. Base64 is readable by anyone. For a private document, redact or password-protect it instead.

Frequently asked questions

Is it free to convert an image to Base64 with Toolezs?

Yes. The tool is free, with no sign-up, no watermark and no limit on how many images you encode.

Are my images uploaded anywhere?

No. The image is read and encoded in your browser, and nothing is kept once you close the tab. The Base64 text never leaves your device unless you copy or download it yourself.

Does Base64 make an image lose quality?

No. Base64 is a text encoding of the file's exact bytes, so decoding gives the same picture back. What does change is size: the encoded text is about 33% larger than the binary file.

When should I avoid a data URI?

Avoid it for large photos, for a picture used on many pages, and wherever caching or lazy loading matters. A data URI cannot be cached on its own and it makes every HTML or CSS file that carries it bigger.

Can I convert Base64 back to an image?

Yes. Open the Image to Base64 tool and switch to the Base64 to Image tab, or open the decoder directly, paste the string and download the picture.

Which image formats can I encode?

PNG, JPG, JPEG, WebP, GIF, BMP, AVIF, SVG and ICO, up to 5 MB per file and 10 files at a time.

Convert an image to Base64 now

Encoding an image should take seconds, not a sign-up form. Open the Image to Base64 converter, drop in your picture and copy the string you need — free, private and finished in your browser. If the result is bigger than you expected, Compress Image or the Image Converter will bring it down before you encode it again.

Share this post

On this page

Start now

Ready to Get Things Done with Toolezs?

Every tool is free, needs no account, and runs on your own device. Pick one, get your result, and get back to what you were doing.

Get Started

No credit card required · No sign-up · No account