In this short guide, we will learn how to decode base64 data in Python using the built-in base64 module. Base64 decoding is essential for handling encoded images, API responses, email attachments, and web data transmission.

Here you can find the short answer:

(1) Decode base64 string to bytes

import base64
decoded_bytes = base64.b64decode('SGVsbG8gV29ybGQ=')

(2) Decode base64 to string

import base64
decoded_string = base64.b64decode('SGVsbG8gV29ybGQ=').decode('utf-8')

(3) Decode base64 image

import base64
with open('image.png', 'wb') as f:
    f.write(base64.b64decode(image_data))

Problem: Understanding Base64 Decoding

Base64 encoding converts binary data into ASCII text format for safe transmission over text-based protocols like HTTP, JSON, and email. Decoding reverses this process to retrieve the original data.

1. Decode Base64 String to Text

The most common use case is decoding base64-encoded text back to its original string:

import base64

encoded_text = 'SGVsbG8gV29ybGQ='

decoded_bytes = base64.b64decode(encoded_text)
decoded_string = decoded_bytes.decode('utf-8')

print(f"Decoded: {decoded_string}")

Output Result:

Decoded: Hello World

Explanation: The .b64decode() method converts the base64 string to bytes, then .decode('utf-8') converts bytes to a readable string.

2. Decode Base64 API Response

Many REST APIs return data in base64 format. Here's how to decode JSON API responses:

import base64
import json

api_response = 'eyJ1c2VyIjogIkpvaG4gU21pdGgiLCAiZW1haWwiOiAiam9obkBleGFtcGxlLmNvbSJ9'

decoded_json = base64.b64decode(api_response).decode('utf-8')
user_data = json.loads(decoded_json)

print(f"User: {user_data['user']}")
print(f"Email: {user_data['email']}")

Output Result:

User: John Smith
Email: [email protected]

Real-world use: Decoding JWT tokens, authentication data, or encrypted payloads from APIs.

3. Decode Base64 Image and Save to File

Base64-encoded images are common in web development and email attachments:

import base64

image_base64 = 'iVBORw0KGgoAAAANSUhEUgAAAAUA...'

image_data = base64.b64decode(image_base64)

with open('company_logo.png', 'wb') as f:
    f.write(image_data)

print(f"Image saved: {len(image_data)} bytes")

Output Result:

Image saved: 8456 bytes

Common use cases: Saving profile pictures, product images, email attachments, or data URI images from HTML.

4. Handle URL-Safe Base64 Encoding

Some systems use URL-safe base64 (replacing + with - and / with _):

import base64

url_safe_encoded = 'SGVsbG8sIHdvcmxkIQ='

decoded = base64.urlsafe_b64decode(url_safe_encoded).decode('utf-8')

print(f"Decoded URL-safe: {decoded}")

Output Result:

Decoded URL-safe: Hello, World!

When to use: Decoding URL parameters, OAuth tokens, or query strings with base64 data.

Error Handling

Always use try-except for robust base64 decoding:

import base64

try:
    decoded = base64.b64decode('InvalidBase64!!!').decode('utf-8')
except Exception as e:
    print(f"Decoding error: {e}")

Output Result:

Decoding error: Incorrect padding