Skip to content

How to Convert Images to Base64 Strings (and Back) in Python

Learn how to encode image files into Base64 strings for embedding in HTML, JSON, or other text formats, and decode them back to image files using Python's built-in base64 module and Pillow library.

Base64 encoding is a common way to represent binary data, like images, as ASCII strings. This is useful for embedding images directly into text-based formats like HTML, CSS, or JSON payloads.

This guide explains to you how to convert image files to Base64 strings and how to convert those strings back into image files using Python's standard library and the popular Pillow library.

Convert an Image File to a Base64 String (Standard Library)

The most straightforward way to convert an existing image file uses Python's built-in base64 module.

Reading and Encoding the Image

The process involves opening the image file in binary read mode ('rb'), reading its raw byte content, and then encoding these bytes using base64.b64encode().

import base64

image_path = 'image.jpg'

try:
    with open(image_path, 'rb') as image_file:
        image_bytes = image_file.read()

        base64_bytes = base64.b64encode(image_bytes)

        print(f"Encoded Base64 (bytes): {base64_bytes[:50]}...") # Print first 50 bytes

except FileNotFoundError:
    print(f"Error: Image file not found at {image_path}")
except Exception as e:
    print(f"An error occurred: {e}")
  • open(image_path, 'rb'): Opens the file for reading (r) in binary mode (b). This is crucial because images are binary data, not text. The with statement ensures the file is closed automatically.
  • image_file.read(): Reads the entire content of the file as a bytes object.
  • base64.b64encode(image_bytes): Takes the bytes object and returns a new bytes object containing the Base64 encoded data.

Decoding Bytes to String

The base64.b64encode() function returns bytes. If you need a standard Python str object (e.g., for JSON serialization), you need to decode the bytes, typically using UTF-8 (which is safe for Base64 characters).

import base64

image_path = 'image.jpg'
base64_string = None # Initialize variable

try:
    with open(image_path, 'rb') as image_file:
        image_bytes = image_file.read()
        base64_bytes = base64.b64encode(image_bytes)

        base64_string = base64_bytes.decode('utf-8')

        print(f"Encoded Base64 (string): {base64_string[:50]}...") # Print first 50 chars

except FileNotFoundError:
    print(f"Error: Image file not found at {image_path}")
except Exception as e:
    print(f"An error occurred: {e}")
  • base64_bytes.decode('utf-8'): Converts the bytes object base64_bytes into a str using the UTF-8 encoding.

Handling File Paths (Especially on Windows)

When specifying file paths, especially absolute paths on Windows, be mindful of the backslash character (\), which is used as an escape character in Python strings.

  • Raw Strings (Recommended on Windows): Prefix the string with r.
    image_path_win_raw = r'C:\Users\Alice\Pictures\image.jpg'
  • Escaping Backslashes: Use double backslashes (\\).
    image_path_win_escaped = 'C:\\Users\\Alice\\Pictures\\image.jpg'
  • Forward Slashes (Works on Windows too): Use forward slashes (/), which Python often handles correctly on Windows for path operations.
    image_path_win_forward = 'C:/Users/Alice/Pictures/image.jpg'

Convert a Base64 String back to an Image File

To reverse the process, you take a Base64 string, decode it back into bytes, and write those bytes to a new file in binary write mode ('wb').

Using the Standard base64 and File I/O

This uses only built-in functions. You'll need the Base64 data, either as a string or bytes.

import base64

base64_string = "/9j/4AAQSkZJRgABAQEASABIAAD/..." # Your actual base64 string

output_path = 'decoded_image.jpg'

try:
    if isinstance(base64_string, str):
        base64_bytes_to_decode = base64_string.encode('utf-8')
    else: # Assuming input is already bytes
        base64_bytes_to_decode = base64_bytes # Use the bytes variable directly

    image_data = base64.decodebytes(base64_bytes_to_decode)


    with open(output_path, 'wb') as output_file:
        output_file.write(image_data)

    print(f"Image successfully decoded and saved to {output_path}")

except base64.binascii.Error as e:
    print(f"Base64 Decoding Error: {e}. Check if the input string is valid Base64.")
except Exception as e:
    print(f"An error occurred during decoding/saving: {e}")
  • base64_string.encode('utf-8'): Converts the Base64 str back into bytes if needed.
  • base64.decodebytes(base64_bytes_to_decode): Decodes the Base64 bytes into the original image bytes. decodebytes is generally preferred over b64decode as it's more lenient with input formatting (like extra whitespace).
  • open(output_path, 'wb'): Opens a file for writing (w) in binary mode (b).
  • output_file.write(image_data): Writes the raw image bytes to the file.

Using Pillow for Decoding and Saving

If you have Pillow installed (pip install Pillow) and want to potentially manipulate or save the image in a different format, you can use Pillow along with io.BytesIO.

import base64
import io
from PIL import Image

base64_string = "/9j/4AAQSkZJRgABAQEASABIAAD/..." # Your actual base64 string

output_path_pillow = 'decoded_image_pillow.png' # Saving as PNG

try:
    if isinstance(base64_string, str):
        base64_bytes_to_decode = base64_string.encode('utf-8')
    else: # Assuming input is bytes
         base64_bytes_to_decode = base64_bytes

    image_data = base64.decodebytes(base64_bytes_to_decode)

    image_bytes_io = io.BytesIO(image_data)

    img = Image.open(image_bytes_io)


    img.save(output_path_pillow)

    print(f"Image successfully decoded using Pillow and saved to {output_path_pillow}")

except FileNotFoundError: # Pillow raises FileNotFoundError if it can't identify format
     print(f"Pillow Error: Could not identify image file format from Base64 data.")
except base64.binascii.Error as e:
    print(f"Base64 Decoding Error: {e}.")
except Exception as e:
    print(f"An error occurred: {e}")
  • io.BytesIO(image_data): Creates an in-memory file-like object containing the raw image bytes.
  • Image.open(image_bytes_io): Pillow reads the image data from this in-memory buffer.
  • img.save(output_path_pillow): Pillow saves the image object to a file. The format is typically inferred from the filename extension (.png in this case), but can be specified explicitly with the format argument.

Encoding In-Memory Pillow Images

While this guide focuses on file conversion, if you have generated or manipulated an image in memory using Pillow and want to get its Base64 representation without saving it to a file first, you can do this:

import base64
import io
from PIL import Image # Assuming you have a Pillow image object 'img'

img = Image.new('RGB', (60, 30), color = 'red') # Example Pillow image

try:
    buffer = io.BytesIO()
    img.save(buffer, format="PNG")
    image_bytes = buffer.getvalue()

    base64_bytes = base64.b64encode(image_bytes)
    base64_string = base64_bytes.decode('utf-8')

    print("In-memory Pillow image encoded to Base64 string.")

except Exception as e:
     print(f"Error encoding in-memory image: {e}")

Why Use Base64 for Images?

  • Embedding: Directly embed images in HTML (<img src="data:image/png;base64,...">), CSS (background-image: url(data:image/png;base64,...)), or other text documents without needing separate image files. Reduces HTTP requests for small icons/images in web pages.
  • Data Transfer: Include image data within text-based data formats like JSON or XML, where raw binary data might cause issues.
  • Databases: Store images in database text fields (though storing as BLOBs or using dedicated file storage is often preferred for performance).

Conclusion

Converting images to Base64 strings in Python is straightforward using the base64 module for encoding (base64.b64encode) and decoding (base64.decodebytes or base64.b64decode).

  • Remember to handle file I/O in binary mode ('rb' or 'wb') and decode/encode between bytes and str as needed using .decode() and .encode().
  • The Pillow library offers additional flexibility for decoding Base64 strings into image objects for manipulation or saving in various formats.
Navigation