To decode base64 format you need to pass a bytes object to the base64.decodebytes function:

import base64

t = "AAADccB6OAAAAAAAAAAAAAAAAAAAAAAAAAA8WIEAAAAAAACxAAAAHgAACAAADAzBmAQwzoMABgCI AiTSSACCCAAhIAAAiAEMTMgMJibMsZuGeijn4BnI+YeQ0OMOKAACAgAKAABQAAQEABQAAAAAAAAA AA=="
t = t.encode("ascii")

decoded = base64.decodebytes(t)

print(decoded)
print(len(decoded)*8)

I get the following:

b'\x00\x00\x03q\xc0z8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00<X\x81\x00\x00\x00\x00\x00\x00\xb1\x00\x00\x00\x1e\x00\x00\x08\x00\x00\x0c\x0c\xc1\x98\x040\xce\x83\x00\x06\x00\x88\x02$\xd2H\x00\x82\x08\x00! \x00\x00\x88\x01\x0cL\xc8\x0c&&\xcc\xb1\x9b\x86z(\xe7\xe0\x19\xc8\xf9\x87\x90\xd0\xe3\x0e(\x00\x02\x02\x00\n\x00\x00P\x00\x04\x04\x00\x14\x00\x00\x00\x00\x00\x00\x00\x00'
920

So 920 bits as expected.

To get data as binary just iterate on the bytes and convert to binary using format and zero-padding to 8 digits (bin adds a 0b header so it's not suitable), and join the strings together:

print("".join(["{:08b}".format(x) for x in decoded]))

results in:

00000000000000000000001101110001110000000111101000111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011110001011000100000010000000000000000000000000000000000000000000000001011000100000000000000000000000000011110000000000000000000001000000000000000000000001100000011001100000110011000000001000011000011001110100000110000000000000110000000001000100000000010001001001101001001001000000000001000001000001000000000000010000100100000000000000000000010001000000000010000110001001100110010000000110000100110001001101100110010110001100110111000011001111010001010001110011111100000000110011100100011111001100001111001000011010000111000110000111000101000000000000000001000000010000000000000101000000000000000000101000000000000000001000000010000000000000101000000000000000000000000000000000000000000000000000000000000000000

(which is 920 chars, as expected)

Answer from Jean-François Fabre on Stack Overflow
Top answer
1 of 2
25

To decode base64 format you need to pass a bytes object to the base64.decodebytes function:

import base64

t = "AAADccB6OAAAAAAAAAAAAAAAAAAAAAAAAAA8WIEAAAAAAACxAAAAHgAACAAADAzBmAQwzoMABgCI AiTSSACCCAAhIAAAiAEMTMgMJibMsZuGeijn4BnI+YeQ0OMOKAACAgAKAABQAAQEABQAAAAAAAAA AA=="
t = t.encode("ascii")

decoded = base64.decodebytes(t)

print(decoded)
print(len(decoded)*8)

I get the following:

b'\x00\x00\x03q\xc0z8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00<X\x81\x00\x00\x00\x00\x00\x00\xb1\x00\x00\x00\x1e\x00\x00\x08\x00\x00\x0c\x0c\xc1\x98\x040\xce\x83\x00\x06\x00\x88\x02$\xd2H\x00\x82\x08\x00! \x00\x00\x88\x01\x0cL\xc8\x0c&&\xcc\xb1\x9b\x86z(\xe7\xe0\x19\xc8\xf9\x87\x90\xd0\xe3\x0e(\x00\x02\x02\x00\n\x00\x00P\x00\x04\x04\x00\x14\x00\x00\x00\x00\x00\x00\x00\x00'
920

So 920 bits as expected.

To get data as binary just iterate on the bytes and convert to binary using format and zero-padding to 8 digits (bin adds a 0b header so it's not suitable), and join the strings together:

print("".join(["{:08b}".format(x) for x in decoded]))

results in:

00000000000000000000001101110001110000000111101000111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011110001011000100000010000000000000000000000000000000000000000000000001011000100000000000000000000000000011110000000000000000000001000000000000000000000001100000011001100000110011000000001000011000011001110100000110000000000000110000000001000100000000010001001001101001001001000000000001000001000001000000000000010000100100000000000000000000010001000000000010000110001001100110010000000110000100110001001101100110010110001100110111000011001111010001010001110011111100000000110011100100011111001100001111001000011010000111000110000111000101000000000000000001000000010000000000000101000000000000000000101000000000000000001000000010000000000000101000000000000000000000000000000000000000000000000000000000000000000

(which is 920 chars, as expected)

2 of 2
16

The easiest way to perform this action using Python 3 is this:

import base64    
base64.b64decode(base64_to_binary_input) 
🌐
Python
docs.python.org › 3 › library › base64.html
base64 — Base16, Base32, Base64, Base85 Data Encodings
Source code: Lib/base64.py This module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data. This includes the encodings specifi...
Discussions

io - How to decode base64 file into binary in Python? - Stack Overflow
I'm building a system which handles pdf file data (for which I use the PyPDF2 lib). I now obtain a base64 encoded PDF which I can decode and store correctly using the following: import base64 # More on stackoverflow.com
🌐 stackoverflow.com
Reddit Gold to Whoever Solve this issue - Converting Base64 Encoded into Binary Found in Python
\xf8mfB]\xc0\xdfC\nB\xc0\x81\xc4\xc5\xf0\xc8\xc3\x9c\xfc\x06D\xbe\x97\x11D\x8dw\x99C\xb2\xdd#C\x10X\xf2B\xe5P\xd1B\x87\x16\xc7B'\xf1\xc2B\xa1/\xb8A9-\xbe@\x02 I think I'm missing something here, in what format is this a representation of binary? More on reddit.com
🌐 r/SQLServer
17
0
June 30, 2023
python - Convert a base64 encoded string to binary - Stack Overflow
I'm trying to convert a base64 encoded string to its binary form. Basically "cw==" should return 01100110000. I tried various modules but can't seem to find a suitable one. Any one got any ideas? More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2017
Python: How do I convert from binary to base 64 and back? - Stack Overflow
So, could anyone point me in the right direction for converting binary numbers to base64 numbers? thanks! ... Do you really want to convert to radix-64 numbers, or base-64 encoding? Converting to radix-64 numbers is a trivial mathematical computation (in any language). ... Just wondering if python ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python
September 4, 2023 - As you can see, the value 20 ... with an online converter. To Base64 encode a string, we convert it to binary sequences, then to decimal sequences, and finally, use a lookup table to get a string of ASCII characters....
🌐
Reddit
reddit.com › r/sqlserver › reddit gold to whoever solve this issue - converting base64 encoded into binary found in python
r/SQLServer on Reddit: Reddit Gold to Whoever Solve this issue - Converting Base64 Encoded into Binary Found in Python
June 30, 2023 -

I am going nuts on this. We have a base64 encoded value:

"+G1mQl3A30MKQsCBxMXwyMOc/AZEvpcRRI13mUOy3SNDEFjyQuVQ0UKHFsdCJ/HCQqEvuEE5Lb5AAg=="

we needed decoded to binary. In python (databricks), we get this which is the correct answer (using

base64.b64decode):

"\xf8mfB]\xc0\xdfC\nB\xc0\x81\xc4\xc5\xf0\xc8\xc3\x9c\xfc\x06D\xbe\x97\x11D\x8dw\x99C\xb2\xdd#C\x10X\xf2B\xe5P\xd1B\x87\x16\xc7B'\xf1\xc2B\xa1/\xb8A9-\xbe@\x02"

However when using this:

SELECT CAST(N'' as xml).value('xs:base64Binary(sql:variable("@str"))', 'varbinary(400)')

I get this:

"0xF86D66425DC0DF430A42C081C4C5F0C8C39CFC0644BE9711448D779943B2DD23431058F242E550D1428716C74227F1C242A12FB841392DBE4002"

which is wrong wrong wrong.

I am pulling my hair out on this one. I will mow your lawn, give your reddit gold, name my third born child after you, you name it, if you can crack this. Thanks for any help in advance.

Find elsewhere
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 272738 › encode-and-decode-binary-data-with-base64
python - Encode and decode binary data with base64 | DaniWeb
See the Python base64 docs and the note on binary modes in Reading and writing files. bumsfeld 413 Nearly a Posting Virtuoso 16 Years Ago ... import base64 # pick sound file you have in working directory # or give full path sound_file = "original.mp3" # use mode = "rb" to read binary file fin = open(sound_file, "rb") binary_data = fin.read() fin.close() # encode binary to base64 string (printable) b64_data = base64.b64encode(binary_data) b64_fname = "original_b64.txt" # save base64 string to given text file fout = open(b64_fname, "w") fout.write(b64_data) fout.close # read base64 string back in fin = open(b64_fname, "r") b64_str = fin.read() fin.close() # decode base64 string to original binary sound object mp3_data = base64.b64decode(b64_str)
🌐
Stack Overflow
stackoverflow.com › questions › 53178799 › base64-to-binary-data-python
image - Base64 to binary data python - Stack Overflow
I'm trying to convert a base64 string to binary data using this code: output = base64.b64encode(requests.get(image_url).content) bin = "".join(format(ord(x), "b") for x in base64.decodestring(outp...
🌐
Medium
medium.com › @ume.cooray › understanding-base64-encoding-in-python-5b31f23dd007
Understanding Base64 Encoding and Decoding in Python | by Umayangana Cooray | Medium
March 18, 2025 - import base64 import json # Example ... In Python, strings need to be converted to bytes before encoding. This is done using .encode('utf-8'). This ensures the string is represented properly in binary form....
🌐
YouTube
youtube.com › neuralnine
Base64 Encoding Binary Files in Python - YouTube
In this video we learn how to encode binary data or files using Base64 in Python.◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾📚 Programming Books & Merch 📚🐍 The Python Bible Book: ht...
Published: June 1, 2023
Views: 18K
🌐
Reddit
reddit.com › r/pythontips › how to convert base64 back to an image in python
r/pythontips on Reddit: How to Convert Base64 Back to an Image in Python
October 29, 2024 -

If you have a Base64 string and you want to turn it back into an image, Python’s base64 library makes this just as easy.

Steps to Create base64 to image Converter in Python

Step 1: Import the Required Library

we will use the built-in base64 library, so make sure to import it:

import base64

Step 2: Get the Base64 String

You need a Base64 string that you want to convert back into an image. This could be one that you’ve stored or received from an API. Here’s a shortened example:

base64_string = "iVBORw0KGgoAAAANSUhEUgAAABAAAAA..."

Step 3: Decode the Base64 String

Once you have the Base64 string, use the base64.b64decode() function to convert it back into binary data.

Step 4: Write the Binary Data to an Image File

Now that you have the binary data, the final step is to save it as an image file. Use the open() function in "write binary" mode ('wb').

with open("output_image.png", "wb") as image_file:
    image_file.write(image_data)

Full Code Example for Converting Base64 to an Image

Here’s the complete Python code that converts a Base64 string back into an image:

import base64  # Step 1: Import the base64 library

# Step 2: Example Base64 string
base64_string = "iVBORw0KGgoAAAANSUhEUgAAABAAAAA..."

# Step 3: Decode the Base64 string back into binary data
image_data = base64.b64decode(base64_string)

# Step 4: Write the binary data to an image file
with open("output_image.png", "wb") as image_file:
    image_file.write(image_data)

Explanation:

  1. base64.b64decode(): Decodes the Base64 string back into binary data.

  2. open("output_image.png", "wb"): Opens a new file in write-binary mode.

  3. image_file.write(): Writes the binary data into the file, creating the image.

🌐
Python
docs.python.org › 3.4 › library › binascii.html
19.8. binascii — Convert between binary and ASCII — Python 3.4.10 documentation
Convert binary data to a line of ASCII characters, the return value is the converted line, including a newline char. The length of data should be at most 45. ... Convert a block of base64 data back to binary and return the binary data.
🌐
Base64encoder
base64encoder.io › python
https://www.base64encoder.io/python/
We cannot provide a description for this page right now
🌐
GitHub
github.com › TheAlgorithms › Python › pull › 12209
Added base64 to binary conversion. by coolsidd · Pull Request #12209 · TheAlgorithms/Python
def binary_to_base64(binary_data): """ 将二进制数据转换为Base64编码 """ return base64.b64encode(binary_data)
Author: TheAlgorithms
🌐
Base64 Decode
base64decode.org
Base64 Decode and Encode - Online
Other variations, usually derived from Base64, share this property but differ in the symbols chosen for the last two values; an example is the URL and filename safe "RFC 4648 / Base64URL" variant, which uses "-" and "_". ... In the above quote the encoded value of Man is TWFu. Encoded in ASCII, the letters "M", "a", and "n" are stored as the bytes 77, 97, 110, which are equivalent to "01001101", "01100001", and "01101110" in base-2. These three bytes are joined together in a 24 bit buffer producing the binary sequence "010011010110000101101110".
🌐
GeeksforGeeks
geeksforgeeks.org › encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python - GeeksforGeeks
August 7, 2024 - So, it is important to handle such kind of malformed data so that it doesn’t crash Python or ... With the help of base64.encodebytes(s) method, we can encode the string using base64 encoded data into the binary form.
🌐
Cryptii
cryptii.com › pipes › base64-to-binary
Base64 to binary: Encode and decode bytes online - cryptii
Base64 encoding schemes are used when binary data needs to be stored or transferred as textual data. Therefore 64 characters are chosen that are both members of a subset common to most encodings (ASCII), and also printable.