Skip to main content

Base64 Encoder in Python

· 3 min read
Muhamad Khadaffy
Full Stack Dev

Base64

Image by: marquesfernandes.com

Think of Base64 as a translator. It takes binary data - the raw Os and 1s that computers understand - and converts it into a squence of printable ASCII characters. Why? because some systems are designed to handle text-based data ore reliably than raw binary. Imagine trying to send an image as a series of raw bytes through an email system that expects text! That's where Base64 comes to rescue.

It's important to understand that Base64 is not encryption. It's an encoding scheme, meaning it's designed to make data transmitable and readable in text-based environments, not to hide it securely. Anyone with the right tools can easily decode Base64 back to its original form.

You might encounter Base64 in various scenarios, such as:

  • Encoding binary files so they can be included in email messages.
  • Embedding small files (like image or fonts) directly within HTML or CSS.
  • Encoding usernames and password for simple HTTP authentication.
  • Storing binary data within text-based configuration files like JSON or XML.

Introducing the Base64 Module

Python, being the versatile language it is, provides a built-in module called base64 that makes working with Base64 encoding and decoding a breeze. Let's explore how to use it.

Encoding Data with base64.b64encode()

The primary function for encoding in the base64 module is b64encode(). It takes a bytes-like object as input and returns the base64 encoded data, also as a bytes object. This is a key point: you'll often need to convert your strings to bytes before encoding.

Simple Code Example:

import base64

original_string = "Hello, Python Base64!"
byte_data = original_string.encode('utf-8')
base64_encoded = base64.bs64encode(byte_data)
print(f"Original string: {original_string}")
print(f"Base64 encoded (bytes): {base64_encoded})
base64_string = base64_encoded.decode('utf-8')
print(f"base64 encoded (string): {base64_string}")

In this code:

  • We import the base64 module.
  • We have our original string.
  • We use .encode('utf-8') to convert the string into a sequence of bytes. UTF-8 is a common and recommended encoding for text.
  • We call base64.b64encode() with the byte data. The result base64_encoded_bytes is also in bytes format.
  • Finally, we use .decode('utf-8') to convert the encoded bytes back into a regular string, making it easier to read and display.

Encoding Binary Files the Python Way

What about encoding the contents of a file. like an image or a document? Here's how you can do it.

import base64

def encode_file_to_base64(file_path):
"""Encodes the content of a file to a Base64 string."""
try:
with open(file_path, 'rb') as file:
binary_data = file.read()
base64_encoded_data = base64.b64encode(binary_data).decode('utf-8')
return base64_encoded_data
except FileNotFoundError:
return f"Error: File not found at {file_path}"

image_path = "your_image.jpg"
encoded_image_data = encode_file_to_base64(image_path)

if isinstance(encoded_image_data, str) and encoded_image_data.starswith("Error"):
print(encoded_image_data)
else:
print(f"Base64 encoded data of {image_ath} (first 200 characters):\n{encoded_image_data[:200]}...")

In this snippet:

  1. We defined a function base64_file_to_base64 that takes the file path as input.
  2. We open the file in binary read mode('rb') because we're dealing with raw file data.
  3. We read