Base64 is a binary-to-text encoding methodology that helps symbolize binary knowledge in ASCII string format. It’s usually used to encode knowledge for transmission over media which can be principally textual content, like emails, JSON-based APIs, and many others., in order that binary knowledge like photos and information don’t get corrupted. The time period Base64 comes from the truth that it makes use of 64 characters – A-Z, a-z, 0-9, +, and / to symbolize knowledge. In recent times, it has been extensively utilized in multimodal AI functions, embedded techniques, cloud-based companies, and net improvement. On this article, we’ll study extra about Base64 and the best way to use it.
Why Base64?
Base64 is generally utilized in instances the place binary knowledge (e.g., photos, movies, mannequin weights, and many others.) must be handed by means of text-based infrastructures with out being altered or corrupted. However why is it a preferred alternative amongst so many different sorts of encodings? Let’s attempt to perceive.
Base64 is:
- Textual content-safe: Can embed binary knowledge in text-based codecs like HTML, XML, JSON, and many others.
- Straightforward to move: No points with character encoding or knowledge corruption.
- Frequent for photos: Typically utilized in net improvement to embed photos straight in HTML/CSS or JSON payloads.
And right here’s how different well-known encodings are in comparison with Base64.
Encoding | Objective | Use Case | Dimension Affect |
Base64 | Binary to textual content | Embedding photos/information in HTML, JSON, and many others. | ~33% improve |
Hex | Binary to Hexadecimal | Debugging, community traces | ~100% improve |
Gzip | Compression | Precise measurement discount for textual content/binary | Compression ratio-dependent |
Additionally Learn: What are Categorical Knowledge Encoding Strategies | Binary Encoding
How Does Base64 Work?
Now let’s attempt to perceive how Base64 works. Right here’s a walkthrough of the step-by-step conversion of the string “Hey” into its Base64 format.
Step 1: Convert the Textual content to ASCII Bytes
Character | ASCII Decimal Worth | Binary Worth (8 bits) |
H | 72 | 01001000 |
e | 101 | 01100101 |
l | 108 | 01101100 |
l | 108 | 01101100 |
o | 111 | 01101111 |
So now, our string “Hey” would appear to be 01001000 01100101 01101100 01101100 01101111.
That’s 5 characters × 8 bits = 40 bits.
Step 2: Break the Binary into 6-bit Teams
Base64 operates on 6-bit blocks, so we group the 40 bits into chunks of 6 which was beforehand in chunks of 8:
01001000 01100101 01101100 01101100 01101111
When these chunks of 8 are grouped in teams of 6 they appear to be this:
010010 000110 010101 101100 011011 000110 1111
Since 40 isn’t straight divisible by 6, now we have to pad some 0s on the finish. We now have 6 full 6-bit blocks and 1 leftover 4-bit block. We pad the final block with 2 zero bits to make it a full 6-bit chunk:
010010 000110 010101 101100 011011 000110 111100
Step 3: Convert 6-bit Teams to Decimal
We all know 2^6 is 64. So, our vary can be in between 0 to 63.
6-bit binary | Decimal |
010010 | 18 |
000110 | 6 |
010101 | 21 |
101100 | 44 |
011011 | 27 |
000110 | 6 |
111100 | 60 |
Step 4: Map to Base64 Characters
Following the usual Base64 character desk, we’ll map our decimal values to the corresponding characters.

Decimal | Base64 Character |
18 | S |
6 | G |
21 | V |
44 | s |
27 | b |
6 | G |
60 | 8 |
We get “SGVsbG8” as our Base64 encoding for our string “Hey”.
Step 5: Add Padding
Since our authentic string had 5 bytes (not a a number of of three), Base64 requires padding with “=” to make the output size a a number of of 4 characters.
5 bytes = 40 bits -> 6 full base64 chars + 2 extra characters (from padded bits) -> Complete 8 characters
Last Base64 encoded string: “Hey” -> SGVsbG8=
Additionally Learn: Full Information on Encoding Numerical Options in Machine Studying
Python Implementation of Base64
Now that you simply perceive how Base64 works, let me present you the best way to implement it in Python. We’ll first attempt to encode and decode some textual content, after which do the identical with a picture.
Encoding and Decoding Textual content
Let’s encode this easy textual content utilizing Base64 after which decode the encoded string again to its authentic kind.
import base64
# Textual content encoding
message = "Hey World"
encoded = base64.b64encode(message.encode())
print("Encoded:", encoded)
# Decoding it again
decoded = base64.b64decode(encoded).decode()
print("Decoded:", decoded)
Output

Encoding and Decoding Photos
In vision-related functions, particularly with Imaginative and prescient Language Fashions (VLMs), photos are sometimes encoded in Base64 when:
- Transmitting photos by way of JSON payloads to or from APIs.
- Embedding photos for coaching and serving multimodal fashions.
- Utilizing CLIP, BLIP, LLaVA or different Imaginative and prescient-Language Transformers that settle for photos as serialized Base64 strings.
Right here’s a easy Python code to encode and decode Photos.
from PIL import Picture
import base64
import io
# Load and encode picture
img = Picture.open("instance.jpeg")
buffered = io.BytesIO()
img.save(buffered, format="JPEG")
img_bytes = buffered.getvalue()
img_base64 = base64.b64encode(img_bytes).decode('utf-8')
print("Base64 String:", img_base64[:100], "...") # Truncated
Output

We are able to additionally decode our base 64 encoded knowledge again to the picture utilizing the beneath code.
from PIL import Picture
import base64
import io
from IPython.show import show, Picture as IPythonImage
# Assume `img_base64` is the base64 string
img_data = base64.b64decode(img_base64)
img = Picture.open(io.BytesIO(img_data))
show(IPythonImage(knowledge=img_data))
Output

To study extra about Base64 and discover many extra encoders and decoders, you may refer this web site.
Issues to Hold in Thoughts Whereas Utilizing Base64
Though Base64 is of nice use in numerous use instances throughout domains, right here are some things to notice whereas working with it.
- Dimension Overhead (~33%): For each 3 bytes of binary, you output 4 bytes of textual content. On giant batches (e.g., hundreds of excessive‑res frames), this could eat community and storage bandwidth rapidly. Think about compressing photos (JPEG/PNG) earlier than Base64 and utilizing streaming if attainable.
- Reminiscence & CPU Load: Changing and buffering a complete picture without delay can spike general reminiscence utilization throughout encoding. Equally, decoding into uncooked bytes after which parsing by way of a picture library additionally provides CPU overhead.
- Not a Compression Algorithm: Base64 doesn’t scale back measurement, it inflates it. All the time apply true compression (e.g., JPEG, WebP) on the binary knowledge earlier than encoding to Base64.
- Safety Concerns: If we blindly concatenate Base64 strings into HTML or JSON with out cleansing, you may open XSS or JSON‑injection vectors. Additionally, extraordinarily giant Base64 knowledge can exhaust the parsers and implement most payload sizes on the gateway.
Conclusion
In an period the place fashions can “see” in addition to “learn”, Base64 has quietly develop into a cornerstone of multimodal techniques. It performs a vital position in knowledge encoding by bridging the hole between binary knowledge and textual content‑solely techniques. In imaginative and prescient‑language workflows, it standardizes how photos journey from cell shoppers to cloud GPUs, whereas preserving reproducibility and easing integration.
Making photos appropriate with text-based infrastructure has all the time been a fancy downside to unravel. Base64 encoding supplies a sensible answer to this, enabling picture transmission over APIs and packaging datasets for coaching.
Login to proceed studying and revel in expert-curated content material.