Base64 to Image

Base64 to image: paste a Base64 string or a data URI and see the picture. The format is read from the first bytes (PNG, JPEG, GIF, WebP, BMP, ICO or SVG), and you can download the file. Decoding happens in your browser, so the image is never uploaded.

Base64 to image
143 bytes decoded · PNG image
The image is drawn from your own bytes in this browser

How to turn a Base64 string into an image

An image stored as Base64 is the file's bytes written as text. To get the picture back, decode the text to bytes and give the bytes to something that can draw them, with the right media type. Paste the string, with or without the data URI prefix, and the tool decodes it, identifies the format from the first bytes, shows the picture and offers the file for download with the right extension.

The preview is created in the browser: the decoded bytes become a temporary object URL, which is set as the source of an image element, and the browser decodes the image itself. The status line shows the size of the decoded file and, once the browser has loaded it, the dimensions in pixels. If the bytes are not an image, the tool says what they are or that the format is unknown.

Which image format a Base64 string holds: the first characters

Every image format begins with a few fixed bytes, a signature or magic number, and because Base64 is a deterministic mapping, those bytes always produce the same first characters. That makes the format readable from the text. The table gives, for each format, the signature bytes in hex and the characters of Base64 that depend only on them.

Image signatures and the start of their Base64
FormatSignature bytes (hex)Base64 starts withMedia type
PNG89 50 4E 47 0D 0A 1A 0AiVBORw0KGgimage/png
JPEGFF D8 FF/9j/image/jpeg
GIF (87a)47 49 46 38 37 61R0lGODdhimage/gif
GIF (89a)47 49 46 38 39 61R0lGODlhimage/gif
WebP (RIFF container)52 49 46 46UklGRimage/webp
BMP42 4DQkimage/bmp
ICO00 00 01 00AAABAimage/x-icon
SVG, written as <svg3C 73 76 67PHN2Zimage/svg+xml

In real files a little more of the prefix is fixed. Every PNG continues with the IHDR chunk, so a PNG string almost always starts iVBORw0KGgoAAAANSUhEUg. JPEG files from cameras and most editors continue with a marker that gives /9j/4AAQSkZJRg for the JFIF variant and /9j/4QA or similar for EXIF. GIF strings start R0lGODdh or R0lGODlh depending on the version. WebP and any other RIFF file begin UklGR; the WebP type follows later in the header.

Because the first characters of Base64 stand for the first bytes of the file, you can read a file's header without drawing it. A PNG file begins with the eight signature bytes, then a chunk called IHDR whose data holds the image size. The chunk starts at byte 8 with a four-byte length (always 13), the four letters IHDR, and then the width and height as four-byte big-endian numbers. The sample image loaded in the tool is a convenient case: its first 32 bytes decode to the values below.

The first 32 bytes of the sample PNG, from its Base64 start iVBORw0KGgoAAAANSUhEUgAAABAAAAAQ
Bytes (offset)HexMeaning
0 to 789 50 4E 47 0D 0A 1A 0APNG signature
8 to 1100 00 00 0DLength of the IHDR data: 13 bytes
12 to 1549 48 44 52The chunk type: the letters IHDR
16 to 1900 00 00 10Width: 16 pixels
20 to 2300 00 00 10Height: 16 pixels
2408Bit depth: 8 bits per channel
2502Colour type 2: truecolour (red, green, blue)

With that table, a Base64 string that begins iVBORw0KGgoAAAANSUhEUgAAABAAAAAQ is a 16 by 16 PNG before any software has opened it. The characters AAAAEAAAABA after IHDR are the two numbers 16 and 16 in Base64 form. This is useful when an image has to be checked against a size limit, or when a string is suspected of being something other than what its label says.

Finding the size of a GIF, BMP or JPEG

Other formats store the dimensions elsewhere. A GIF puts the width and height right after the six-byte signature, as two 16-bit little-endian numbers at bytes 6 to 9, so GIF89a followed by 10 00 10 00 is a 16 by 16 image. A BMP stores its width at byte 18 and its height at byte 22, as 32-bit little-endian numbers, after a 14-byte file header and the start of the DIB header. JPEG is more awkward: the size lives in a frame marker (FF C0 and its relatives) whose position depends on the metadata segments before it, so a program must walk the file.

The tool shows the pixel size of any format after the browser has loaded the image, which is the easiest way. The byte offsets matter when no browser is available, in a server-side check for example, and the PNG and GIF layouts are simple enough to read without a library. Byte order is the trap: PNG is big-endian and GIF and BMP are little-endian, so the same size is written 00 00 00 10 in one and 10 00 in the other.

Data URI syntax

A data URI packs the media type and the data into one string that a browser treats as if it were a file address. The scheme data: is followed by the media type, then ;base64 to say that the payload is Base64, then a comma and the payload. The media type is optional and defaults to text/plain, but for images it must be present and correct. The syntax is defined in RFC 2397.

Parts of a data URIA data URI is data: then the media type image/png, the flag ;base64, a comma, and the Base64 payload.data:schemeimage/pngmedia type;base64encoding flag,separatoriVBORw0KGgo…the bytes, Base64-encoded
RFC 2397 syntax. The media type is optional (it defaults to text/plain), and without ;base64 the payload is percent-encoded text instead.

Without ;base64 the payload is percent-encoded text, which is how small SVG files are often embedded. The tool reads both forms. When the string has a media type that disagrees with the bytes, for example image/jpeg on PNG data, the bytes win in the preview, but a browser given the same mismatch may refuse to render, so correct the type before using it in a page.

Using a Base64 image in HTML and CSS

With the complete data URI, an image can be used anywhere a URL is accepted. In HTML it goes in the src attribute of an image tag; in CSS it goes inside url().

HTML and CSS
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2…" alt="A 16 by 16 pixel test image" width="16" height="16">

.icon { background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQ…"); }

Keep the alt text and the dimensions: a data URI image still needs them for accessibility and layout stability. Quote the URL in CSS, because a Base64 string can contain / and + and some unquoted forms break parsers. Under a strict Content Security Policy, the img-src or default-src directive has to list data: or the browser blocks the image without showing it.

Why a Base64 image does not show

Most failures are visible in the text. The table covers the causes that come up most, with the symptom in the tool and in a browser.

Common reasons a Base64 image fails
CauseWhat you seeFix
String cut offThe top of the picture loads and the rest is missing or grey (JPEG), or the image fails entirely (PNG)Copy the whole string; check the final characters and the = padding
Quotes, commas or a field name copied tooInvalid character at position 1 or at the endSelect only the characters of the string
Prefix repeatedThe text starts data:image/png;base64,data:image/png;base64,Remove one prefix
Plus signs became spacesInvalid character where a + should beReplace the spaces with +
Line breaks or JSON escapesSlashes appear as \/ and newlines as \nUnescape the JSON string first; real line breaks are ignored
Wrong media typeThe tool shows the image, the browser does notSet image/png, image/jpeg and so on to match the bytes
Data encoded twiceThe decoded bytes start with a Base64 string againDecode once more
Not an imageThe tool names another file type, or noneUse the matching tool for PDFs and other files

Image formats at a glance

PNG is lossless, supports transparency and is the usual choice for icons, screenshots and diagrams; its signature also contains a line-ending check (0D 0A 1A 0A) that exposes files damaged by text conversion. JPEG is lossy and suits photographs, and its three-byte signature FF D8 FF is the shortest of the group. GIF supports animation and a 256-colour palette, and the browser draws its first frame in the preview.

WebP stores lossy and lossless images and animation in a RIFF container, which is why the signature RIFF is shared with WAV audio and the file type is only known from the bytes at offset 8. BMP is uncompressed and large, and its two-byte signature BM is so short that plain text can start with it. ICO holds favicons. Modern browsers also draw AVIF files, which are not in the preview's list of named formats but display if the browser supports them.

SVG in a data URI: Base64 or plain text?

SVG is XML text, so it does not need Base64. A data URI can hold it as percent-encoded text, which is smaller than the Base64 form by about a quarter, and it stays readable in the stylesheet. Base64 is common anyway because it avoids the escaping of quotes, angle brackets and the # sign, and tools produce it by default. A string that starts PHN2Zy is the text <svg in Base64, and PD94bWwg is <?xml followed by a space.

If you control the file, prefer the text form with only the characters that need escaping replaced, and use Base64 when a tool or API requires it. The preview draws SVG through an image element, which means scripts inside the file do not run, but embedded SVG in a page is different, as the security section explains.

Size and limits

The decoded file is three quarters the size of the string, less padding. A string of 1,368 characters is 1 KiB of data and a string of one million characters is about 750 KB. To estimate the image size from the text, multiply the length by 3, divide by 4 and subtract the number of = signs at the end. The tool shows the exact count.

Memory is the other cost: a browser decodes the image to raw pixels to draw it, so the memory used is the width times the height times four bytes, whatever the file size. A 4,000 by 3,000 pixel photograph is about 48 MB of pixels once drawn, which is why a modest string can still be heavy for a phone. Browsers accept long data URIs in image sources, and limits in practice come from elsewhere: some servers cap request and header sizes, email clients strip or block data URI images, and a stylesheet that embeds large images blocks rendering while it downloads. As a rule of thumb, embedding is reasonable for files of a few kilobytes and a poor idea above about 10 KB, a point the image to Base64 page discusses in more detail.

Images that come from APIs and databases

Services that return images in JSON usually give a bare Base64 string and the media type in a separate field, so the prefix has to be added by the client. Others return the complete data URI. Both are accepted here. Databases that store images as text columns often store the Base64 with line breaks every 76 characters, which the decoder ignores.

Look at the first characters before anything else. A string that starts with /9j/ is a JPEG even if the field says png, and one that starts with a 4 or a hex-looking run of letters may be encoded twice or in hex. If the string begins with a quote or a bracket, it is a piece of JSON and not the data itself. For a string you do not recognize at all, the auto decoder compares it with hex, URL encoding and the other formats.

Security of Base64 images from unknown sources

An image that comes from a stranger is data, and data can be hostile. The tool draws every image through an image element and not as inline markup. An SVG that is loaded as an image cannot run scripts or load other resources, so the preview is safe, but the same SVG pasted into a page as inline markup, or opened as a document on its own, can execute script. When you build a page from untrusted Base64, keep it in an img tag and never in innerHTML.

Servers that accept Base64 uploads should decode them and check the signature against the allowed types, rather than trust the declared media type, and re-encode the image with a library before serving it, which strips metadata and anything trailing the image data. A declared image/png that decodes to something else is the standard sign of an upload attack, which is why the tool names the format it finds.

Base64 to image in JavaScript, Python, PHP and the shell

JavaScript
// Browser: show it
img.src = "data:image/png;base64," + b64;

// Node.js: save it
require("fs").writeFileSync("out.png", Buffer.from(b64, "base64"));
Python
import base64
open("out.png", "wb").write(base64.b64decode(b64))
# if the string has a data URI prefix: b64.split(",", 1)[1]
PHP and the command line
file_put_contents("out.png", base64_decode($b64));

base64 -d image.txt > out.png          # Linux, Git Bash
[IO.File]::WriteAllBytes("out.png", [Convert]::FromBase64String($b64))   # PowerShell

Whatever the language, check the signature of the file you wrote: the first eight bytes of a PNG are 89 50 4E 47 0D 0A 1A 0A, and file out.png on Linux and macOS names the format.

Performance and accessibility of inline images

An image in a data URI is part of the document or stylesheet that contains it. The browser cannot start downloading it early, cannot cache it separately, and has to parse the whole string before the rest of the stylesheet is usable. For the image that determines the largest contentful paint of a page this is usually worse than a file the browser can discover and fetch early, which is why the hero image should not be inlined.

Accessibility rules do not change. An image that carries information needs an alt text that says what it shows, and a decorative one gets an empty alt. Give images explicit width and height so that the layout does not move while the page loads, and add loading and decoding hints (loading="lazy" and decoding="async") to inline images below the fold. A large inline string in an HTML attribute also makes the source hard to read and edit, so keep such strings in a stylesheet, a script or a data file where they can be left alone.

Patterns that use small inline images

A few uses are good ones. A tiny blurred preview of a photograph, 20 pixels wide and a few hundred bytes, can be inlined as a placeholder while the full image loads. A favicon can be set with a data URI in the link element, which is handy for a single-file page. Small repeated icons and decorative patterns in a stylesheet avoid a handful of requests on slow connections. Test pages, bug reports and examples that need to be self-contained are the strongest case, and the tool's sample image exists for that purpose.

Fonts are the other file type that is often inlined, as woff2 data in a @font-face rule. The same trade-off applies in a stronger form, since a font is usually larger than an icon and a stylesheet that contains one cannot render until the font bytes have arrived.

Animated and multi-frame images

GIF, APNG and WebP can contain several frames, and the browser plays them. The preview shows an animation as the browser draws it, so a decoded animated GIF moves. The download is the original bytes with every frame, and nothing is lost by the round trip through Base64. For a PNG, the signature is the same for plain and animated files; the animation chunk (acTL) sits after the header, so the first Base64 characters give no hint.

Changing format after decoding

The tool does not convert between image formats. It gives back the same bytes in the same format. To convert, save the decoded file and open it in an editor, or draw it in a browser and re-encode with a canvas, which can produce PNG, JPEG or WebP at a chosen quality. Re-encoding changes the file: a JPEG decoded and saved again at the same nominal quality is not identical, and transparency is lost when going from PNG to JPEG, which has no alpha channel.

Metadata, orientation and colour in a decoded image

The decoded file keeps everything the original had. A JPEG may carry an EXIF block with the camera, the date and, for photos taken on a phone, the location. EXIF also stores an orientation flag: some images are stored rotated and displayed upright because of it. Modern browsers apply the flag when they draw the image, so the preview here shows the picture as people would see it, while a program that ignores the flag shows it sideways.

PNG files can contain a colour profile and text chunks, and animated formats carry timing data. None of this changes the Base64, which only transports the bytes. When you publish a decoded photograph, strip location and device data first, since the Base64 string is as revealing as the file.

Formats the browser cannot draw

The tool names formats it recognizes from the signature, and the preview depends on what your browser can render. TIFF files are identified by their header (49 49 2A 00 or 4D 4D 00 2A) but most browsers do not draw them. HEIC images from phones and RAW files from cameras are other examples that browsers draw poorly or not at all. AVIF and WebP are drawn by current browsers. When the tool reports a known format and shows no picture, download the file and open it in an application that supports the format.

If the bytes match no signature at all, the string is not an image in any of the listed formats, and the first bytes shown in hex are the best clue to what it is. A common surprise is a string that decodes to text, such as an HTML error page or a JSON message returned by a failed API call and then Base64-encoded by mistake.

Base64 images in email, documents and Markdown

Email clients treat inline images unevenly. Some show data URIs in HTML mail, many block them, and attachments referenced by a Content-ID are the dependable method. Word and PowerPoint accept pasted pictures, not data URIs in fields. Markdown renderers differ, and a data URI in an image link is often removed for security: GitHub, for one, sanitizes it. Test in the target environment before relying on it.

When you receive a Base64 image from one of these systems, the string usually sits inside HTML or XML. Copy the attribute value only, without the src= or the quotes, and paste it here; the prefix is optional. The data is exactly the original file, so the round trip through Base64 preserves everything, including metadata, which can be a privacy concern for photographs that carry location data.

Declared type against real type

The media type in a data URI is a claim. Browsers mostly believe it for images and draw the data with the matching decoder, and some sniff the content when the claim and the bytes disagree, though the rules are not uniform. An image labeled image/png that contains JPEG data will often still show in the preview here, because the tool goes by the bytes, but may fail elsewhere. Correct labels also affect caching, accessibility tools and content security checks.

Practical advice: derive the media type from the signature, as the tool does, not from the file name or from what a source says. When writing code that serves or stores such images, keep a short allow-list of signatures (PNG, JPEG, GIF, WebP) and reject the rest.

A checklist for an image that will not appear

  1. Paste the string into the tool. If it reports an invalid character, fix that first; the position is given.
  2. Read the status line. If it names a format, the Base64 is fine and the problem is in the page that uses it.
  3. If it says no known signature, look at the first characters. A string starting with eyJ is JSON, one starting with H4sI is gzip data, and one starting with a hex-looking run may be hex and not Base64.
  4. Check the length. A decoded size far smaller than the original file points to truncation.
  5. Compare the declared media type in your page with the format the tool found.
  6. In a stylesheet, quote the URL and check for characters in the string that your build tool escapes or rewrites.
  7. Under a Content Security Policy, make sure img-src allows data:.

Questions about Base64 images

How can I tell what kind of image a Base64 string is?

Look at the start of the string. iVBORw0KGgo is a PNG, /9j/ is a JPEG, R0lGOD is a GIF, UklGR is a WebP and PHN2Zy is an SVG written as text. The decoder reads the same signature from the decoded bytes and names the format.

Do I need the data:image/png;base64, prefix?

Not in this tool: both a bare Base64 string and a full data URI are accepted. In HTML and CSS the prefix is required, because the browser needs the media type to know what the bytes are.

Why is the image blank or broken?

The usual causes are a truncated string, extra characters such as quotes or a second data: prefix, spaces where + signs should be, or a media type that does not match the bytes. The tool reports an invalid character with its position and says if the decoded bytes are not an image.

Is the image uploaded anywhere?

No. The string is decoded in your browser and the preview is drawn from a local object URL. Nothing is sent to a server, and the page works offline once loaded.

How large can the image be?

There is no limit set by the tool; the limit is browser memory. A Base64 string is about a third longer than the image file, so a 3 MB picture is a string of roughly 4 million characters, which most browsers handle without trouble.