
We use variable read_64 to read encoded values stored in the decoder variable.

We have Base64 values in the coded_str variable.Let’s recall the steps initiated so far to make everything crystal clear. jpeg file where we will be storing our decoded Base64 values.įinally, we decode and write the contents into a new image file. The variable final_decoder is used to create a new writable. Then the contents of the decoder are read by a variable using the syntax, read_b64 = decoder.read(). The file is loaded as a readable entity because we won’t be writing anything in this file anymore. bin file through the syntax decoder = open('pic_encoding.bin', 'rb'). Variable decoder is created that loads the. Decode Base64 Values and Write Into an Image File 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.If not, we print an error message.Īnd that's it! With these two steps, we can read and send an image as a base64 string to an API using Python.
#Python ecode image in base64 encoding code
If the status code is 200, we print a success message. We are setting the "Content-Type" header to "application/json" and passing the encoded image as a JSON object in the "data" parameter.Īfter sending the request, we check the status code of the response to see if the image was uploaded successfully.

In this example, we are sending a POST request to the API endpoint " ". Print(response.text) # or get json response using response.json() Response = requests.post(url, headers=headers, json=data) Here is an example code to send the request: import requests We will be using the requests library in Python to do this. Now that we have the image encoded as a base64 string, we can send the API request. The resulting string is stored in the encoded_string variable. We then read the contents of the file and encode it as a base64 string using the b64encode() method from the base64 library. In this example, we are opening the image file in binary mode using the "rb" parameter. With open(image_path, "rb") as image_file:Įncoded_string = base64.b64encode(image_file.read()) Here is an example code to encode the image: import base64 We can use the base64 library in Python to do this. Let's get started! Read and Encode Imageįirst, we need to read the image we want to send to the API and we need to encode the image as a base64 string. We will be using the requests library in Python to send the API request and the base64 library to encode the image as a string. This is commonly used in web applications to transmit images or other data over the internet. Base64 encoding is a way of representing binary data in ASCII text format.
#Python ecode image in base64 encoding how to
In this article, we will discuss how to read and send an image as a base64 string to an API using Python.
