Image to Base64 Converter
Turn a PNG, JPG, GIF, WebP, SVG or any other image into a Base64 data URI, raw Base64, an HTML img tag or a CSS background. Choose, drop or paste a screenshot. Switch direction to turn Base64 back into an image.
data:image/png;base64,iVBORw0KGgo…
tick.png
PNG · 142 bytes
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAACVBMVEUAAAAGttT///9jNf+1AAAAAXRSTlMAQObYZgAAADNJREFUeNpjYAAB1tDQAAbR0NAQFCIMRESBiMwQBtGpS0MYRJdNDWEQzQLJRqIoBhsAAgB8/xA3iRNHSAAAAABJRU5ErkJggg==
How to convert an image to Base64
- Choose your image, drag it onto the box, or copy an image or screenshot and press Ctrl+V (⌘+V on a Mac). It is read in your browser, so nothing is uploaded.
- Pick the format you need: a data URI for web pages, raw Base64 for an API or JSON field, or a ready-made img tag or CSS rule.
- Copy the result, or save it as a .txt file if it is long.
The tool reads PNG, JPG, GIF, WebP, SVG, AVIF, ICO, BMP, TIFF and HEIC. It checks what the file really is from its first bytes, not its name, so a JPEG saved as photo.png still gets the right image/jpeg label. Base64 copies the file exactly: nothing is resized or recompressed, and GIF animation and PNG transparency stay as they are.
Example: a 16 × 16 icon as a data URI
The example in the tool is a small tick icon saved as a PNG, 142 bytes long. As a data URI it fits in one line of HTML, so the page shows the icon without loading a separate file:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAgMAAABinRfyAAAACVBMVEUAAAAGttT///9jNf+1AAAAAXRSTlMAQObYZgAAADNJREFUeNpjYAAB1tDQAAbR0NAQFCIMRESBiMwQBtGpS0MYRJdNDWEQzQLJRqIoBhsAAgB8/xA3iRNHSAAAAABJRU5ErkJggg==" alt="Done" width="16" height="16">
- 142 bytes are split into groups of 3: 48 groups (the last one is short).
- Each group becomes 4 characters: 48 × 4 = 192 characters, ending in
==padding. - The
data:image/png;base64,prefix adds 22 more.
Result: 142 bytes became a 214-character data URI. Give the img tag a real alt text, just as you would for a normal image file.
Which output to use: HTML, CSS, JSON or a favicon
| Where it goes | Format | Looks like |
|---|---|---|
| HTML image | Data URI | <img src="data:image/png;base64,…" alt="…"> |
| CSS background | Data URI | background-image: url("data:image/png;base64,…"); |
| JSON or API field | Raw Base64 | "image": "iVBORw0KGgo…" |
| Favicon | Data URI | <link rel="icon" href="data:image/png;base64,…"> |
The most common mix-up is the prefix. Browsers need data:image/…;base64, in front, while most APIs want raw Base64 and reject the prefix. Some APIs ask for the image type in a separate field, such as "media_type": "image/png".
When to use a Base64 image (and how big it gets)
Base64 is about a third bigger than the image file, because every 3 bytes become 4 characters. It saves the browser a separate download, but the image can no longer be cached on its own. Every page that includes it downloads it again, and the HTML or CSS has to load fully before anything shows.
That makes it a good fit for small images of a few KB: icons, logos, loading spinners, placeholder images and single-file HTML reports. It also suits API and JSON fields that only accept text. Photos, banners and any image used on many pages are better as normal files.
| Image | File size | Base64 | Base64 a good idea? |
|---|---|---|---|
| Small icon | 1 KB | 1,368 characters | Yes |
| Logo | 10 KB | 13,656 characters | Usually |
| Web banner | 100 KB | 136,536 characters | Better as a file |
| Phone photo | 3 MB | 4,194,304 characters | Only for an API that needs it |
KB and MB here are 1,024 and 1,048,576 bytes. Add the prefix length (22 characters for PNG) for a data URI. If the Base64 is too long for where it has to go, shrink or compress the image first: re-encoding the same file never makes it shorter.
Image to Base64 in JavaScript, Python, PHP and the command line
JavaScript: a file the user picks (gives a data URI)
const file = document.querySelector('input[type=file]').files[0];
const reader = new FileReader();
reader.onload = () => console.log(reader.result); // data:image/png;base64,iVBORw0KGgo…
reader.readAsDataURL(file);JavaScript: an image URL
const blob = await (await fetch('https://example.com/logo.png')).blob();
const reader = new FileReader();
reader.onload = () => console.log(reader.result);
reader.readAsDataURL(blob);Fetching an image from another site only works if that site allows it (CORS). If it fails, download the image and use the tool above.
Node.js (gives raw Base64)
const fs = require('fs');
const base64 = fs.readFileSync('logo.png').toString('base64');Python
import base64, mimetypes
with open('photo.jpg', 'rb') as f: # 'rb': read as bytes, not text
b64 = base64.b64encode(f.read()).decode('ascii')
data_uri = f"data:{mimetypes.guess_type('photo.jpg')[0]};base64,{b64}"PHP
$b64 = base64_encode(file_get_contents('logo.png'));
$dataUri = 'data:' . mime_content_type('logo.png') . ';base64,' . $b64;Command line
base64 -w 0 logo.png > logo.txt # Linux (-w 0 = one line)
base64 -i logo.png -o logo.txt # macOS
[Convert]::ToBase64String([IO.File]::ReadAllBytes("C:\images\logo.png")) | Set-Content logo.txt # Windows PowerShellThe usual mistake is reading the image as text (Python's 'r' instead of 'rb'), which damages the bytes. Windows' certutil -encode also works, but it adds BEGIN/END lines and line breaks that you must remove before using the result in HTML.
Turning Base64 back into an image
Switch the tool to Base64 → Image and paste the data URI or the raw Base64. It copes with line breaks, missing = padding, URL-safe - and _, and a leftover src="…" around it. It works out the format, shows a preview and lets you download the file with the right extension.
The first characters tell you the format before you decode anything:
| Base64 starts with | Format |
|---|---|
| iVBORw0KGgo | PNG |
| /9j/ | JPEG |
| R0lGOD | GIF |
| UklGR | WebP |
| PHN2Zy or PD94bWw | SVG (<svg or <?xml) |
| AAABAA | ICO (favicon) |
| Qk | BMP |
base64 -d logo.txt > logo.png # Linux / macOS
open('logo.png', 'wb').write(base64.b64decode(b64)) # PythonFrequently asked questions
Does converting an image to Base64 reduce its quality?
No. Base64 writes out the exact bytes of the file as text, so decoding it gives back the identical image. It is not compression either: the text is about a third bigger than the file.
Can I convert an iPhone HEIC photo to Base64?
Yes, the tool reads HEIC. But only Safari can show HEIC images in a web page, so for a website or email, convert the photo to JPG first and then to Base64.
Should I use Base64 for an SVG?
Often you do not need to. SVG is already text, so it can go into a data URI URL-encoded instead, which is usually shorter. When you load an SVG, the tool offers this as a checkbox and shows how many characters it saves.
Do Base64 images work in email?
In most mail apps, yes: Apple Mail, Gmail, Yahoo and Outlook show them, but Outlook 2007–2019 for Windows does not show Base64 GIFs. Keep them small, as large emails get clipped or flagged, and use a linked image when it must show everywhere.
One format only: GIF to Base64 · WebP to Base64 · Base64 as bytes: Base64 to Hex
Sources
- MDN: data: URLs: how data URIs are written and where browsers accept them.
- MDN: Base64: why Base64 makes data about a third larger.
- MDN: FileReader.readAsDataURL(): the browser method in the JavaScript examples.
- Can I email: Base64 images: which mail apps show Base64 images.
- Can I use: HEIF/HEIC: which browsers can display HEIC images.
Related Tools
Build and preview Valorant crosshair settings.
Search common Vim commands.
Turn JSON rows into bar, line, or pie charts.
Convert WebP files and Base64 data in both directions.
Convert GIF files and Base64 data in both directions.
Save any GIF frame, or all frames, as JPG.