
It should be made sure that the file is in the same directory where the python.txt file is stored, or the system won’t interact with it. txt file can also be used.Īll it needs is to put the file having Base64 values in the syntax, with open('(filename.extension)', "wb") as file:, and the file will be loaded in the program. The above program can be used to recreate the coded_str variable, but a. Here, the variable coded_str is used in the above program. The file.write(coded_str) syntax simply writes those Base64 values into that. bin file, in which we store the Base64 values. The syntax with open('file_name, "wb") as file: creates a writable ( "wb"). bin file is created to store the Base64 values for this step. Decoding and writing that data into an image file.
#Python base64 decode code#
Decode a Base64 Code to Its Image Form in Pythonĭecoding an image file requires two main steps: You can execute the above code to produce the result on your machine. With open("cheers.jpg", "rb") as photo2code:Ĭoded_str = base64.b64encode(photo2code.read()) The syntax photo2code.read() reads the byte values of the image, and the function base64.b64encode(objectname.read() converts the byte values into Base64 coded values.įinally, coded_str is printed to see the Base64 output. The Base64 coded values of the image are stored in the variable coded_str. The syntax with open("cheers.jpg", "rb") is a Base64 function that stores the image’s bytes in an object named photo2code that we created. We have an image named cheers.jpg which will be encoded. We will look at how images are encoded into their Base64 codes using Python and how it is decoded back, as well. The last segment will discuss practical uses of Base64 decodings, such as Base64 image encoding and decoding. To see the result, we print the variable decoded_value.ĭecoded_value = base64.b64decode(input_string).decode('utf-8').Other interpreting formats can be chosen as well. The string is interpreted into its 8-bit binary values.The method used to interpret this string is UTF-8, which is very similar to ASCII. We use the variable decoded_value to convert the input string to its corresponding binary array values.

The variable input_string is used to store the value that needs to get decoded.


We need to print the variable decimal_converted_string to see the decoded result.īase64_converted_string = input_string.encode("ascii")ĭecode = base64.b64decode(base64_converted_string)ĭecimal_converted_string = code("ascii") In the program below, decode is the variable that stores the Base64 decoded value.

base64.b64decode(base64_converted_string) The syntax below converts the ASCII values to their Base64 values. In the 4th step, we use the variable decode to convert ASCII values into their Base64 value.The variable, base64_converted_string, takes the value ‘U3Vubnk=’ and turns it into its ASCII values.Declare a string variable input_string and put the encoded value ‘U3Vubnk=’ as input in it.This library package aids in encoding-decoding Base64 numbers in Python.
