How-to

How to Convert an Image to Base64 and Back: 4 Ways

You need to drop a logo straight into an email, bake an icon into your CSS, or pass a picture through JSON — and for all of that the image has to become Base64. Here are four working ways, from a one-click converter to three lines of code. And, crucially, how to turn the string back into a normal file.

Dense colourful code on a black screen — encoding an image to Base64
Encoding an image to Base64 takes seconds — you just need the right tool. Photo: Pexels

When you actually need this

Encoding an image to Base64 makes sense when the picture has to live inside text rather than as a separate file. Typical cases: an icon baked into CSS to avoid an extra request; a logo in an HTML email that shows even when external images are blocked; a small avatar passed through an API in JSON. In all of these you need a string, not a file.

The principle is the same in every method: image bytes go in, and out comes text made of 64 characters that's about a third longer than the original. Only the tool changes — sometimes a button in the browser, sometimes three lines of code. So decide first where you need the string — on a page, in a script or in an API — and the method picks itself.

If you simply want to shrink or convert a photo for a website, Base64 is not your tool. We dig into where embedded images help and where they hurt in "Base64 images on the web."

Base64 in 10 seconds

It is a way to write an image's bytes using 64 safe characters (letters, digits, + and /). The image does not change — only its written form does. For a deeper dive, start with "What is Base64."

Way 1: an online converter (the simplest)

If you do not need code, this is the best path. Upload a PNG, get a ready data URI string, copy it. It works in any browser, including on a phone, and requires no installation.

1

Open the converter

Go to the PNG → Base64 page and drag your file into the upload area.

2

Copy the result

The converter returns a data:image/png;base64,… string — hit "Copy."

3

Paste into code

Drop the string into an image src or a CSS url() property.

Encode an image in one click

Upload a PNG to the FormatZ converter and get a ready Base64 string in a couple of seconds — right in your browser, with no install and no sign-up.

Open the PNG → Base64 converter

Way 2: JavaScript right in the browser

If you write code, the browser has a built-in helper called FileReader that does this for you — the snippet below is for developers; if that's not you, the online converter above is all you need. When a user uploads a picture and you want to encode it on the fly, its readAsDataURL method returns a ready data URI string immediately, no extra tools required.

const input = document.querySelector('input[type=file]');
input.addEventListener('change', () => {
  const reader = new FileReader();
  reader.onload = () => {
    console.log(reader.result);
    // → "data:image/png;base64,iVBORw0KGgo..."
  };
  reader.readAsDataURL(input.files[0]);
});

Developers also have two tiny built-in commands — btoa() and atob() — for quick text conversions, but for actual image files FileReader is the easier route: it builds the data:…;base64, prefix for you.

Colourful Python code with try and except blocks on a screen
In Python, encoding and decoding Base64 are two lines from the standard module. Photo: Pexels

Way 3: Python and the command line

If the image needs to be handled on a server or in a script, the simplest choice is Python's standard base64 module. Open the file in binary mode (rb), encode it, and optionally add the data URI prefix:

import base64

with open("logo.png", "rb") as f:
    data = base64.b64encode(f.read()).decode()

uri = "data:image/png;base64," + data
print(uri)

Terminal fans can use the base64 utility straight from the Linux and macOS command line: base64 logo.png > logo.txt encodes the file, and the -d flag (base64 -d logo.txt > logo.png) decodes it back. Fast, and not a single line of code.

A common task of its own is passing an image through JSON via an API. Plain JSON can't store binary data, but it stores strings beautifully. So the image is encoded to Base64 and placed as an ordinary text field:

{
  "user": "anna",
  "avatar": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUg..."
}

On the server side, that string is decoded back into a file with the same base64.b64decode. This is exactly how avatars and small attachments often travel between the front end and the back end.

Close-up of colourful JavaScript code — embedding a Base64 string into markup
All that's left is to paste the ready string into the right place — in HTML or in CSS. Photo: Pexels

Way 4: pasting into CSS and HTML

Once you have the string, you just need to use it correctly. In HTML it goes straight into the src attribute; in CSS, inside url():

<!-- HTML -->
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Logo">

/* CSS */
.icon {
  background: url("data:image/svg+xml;base64,PHN2Zy...") no-repeat;
}

Note the type: SVG uses image/svg+xml, JPG uses image/jpeg. Specify the wrong type and the browser may refuse to render the image. Here's a cheat sheet of the most common types:

PNG
image/png
JPG / JPEG
image/jpeg
SVG
image/svg+xml
GIF
image/gif
WebP
image/webp
Prefix
data:<type>;base64,

For more on using it in styles and emails, see "Base64 in CSS and email."

The real secret to a clean string is the right data type in the prefix. Get it wrong, and the browser simply won't understand it's looking at an image.

Not for heavy images

Remember the roughly 33% size growth. Encode only small icons and logos to Base64; leave large photographs as separate files — that way they cache and load faster.

How to decode it back

The reverse direction is getting a normal file out of the string. The simplest path is again online: paste the string (with or without the data: prefix) into the Base64 → PNG converter and download the image.

In Python it is symmetric: base64.b64decode(string) returns bytes that you write to a file in wb mode. The one thing to watch — first strip the data:image/png;base64, prefix if it is present: the decoder only wants the data itself.

4working methods
~3lines of Python
0quality loss

Common mistakes

  • Forgetting to strip the prefix. The decoder wants clean Base64, but you handed it the whole data:image/png;base64,… — and got a broken file. Remove everything up to the comma.
  • Wrong type label. The little image/png tag (its MIME type) must match the real format — write it for JPG data and the browser refuses to draw the image.
  • Line breaks. Some tools insert newlines every 76 characters. For a data URI, better remove them — otherwise the string may fail in HTML.
  • Huge files. You encoded a 5 MB photo and the page bloated. For large images you do not need Base64.

Want your file back right now? Use the Base64 → PNG converter, and the full list of conversions is always on the all formats page.

An online converter. You upload a PNG and get back a ready data URI string you can copy straight into your code. Nothing to install, and it works on any device — phones included.
In the browser, JavaScript's FileReader does it via readAsDataURL, which returns a data URI string directly. In Python, use the base64 module: open the file in rb mode and call base64.b64encode. Both approaches give the same result.
The easiest way is the online Base64 to PNG converter: paste the string (with or without the data URI prefix) and download the file. In Python it is base64.b64decode, and you write the result to a file in wb mode.
Yes, if your tool expects clean Base64. The data:image/png;base64, prefix is part of the data URI, not the data itself. A good online converter usually understands both forms and trims the prefix for you, but in code you have to remove it manually.
No. Base64 does not compress or re-save the image — it only rewrites the same bytes as text. The pixels before and after are identical; only the size of the representation changes: the string is about a third longer than the file.