Base64 decode to image python

Convert Base64 To Image In Python

Last updated June 16, 2023 by Jarvis Silva In this tutorial I will show you how to convert base64 to image file using python, To convert base64 to image we will use the python library Base64 it allows us to decode base64 code, It comes preinstalled with python so you don’t have to install it.

Python Code To Convert Base64 To Image

 # Import base64 module import base64 # Get the base64 file data with open('base64.txt', 'rb') as bs64_file: bs64_data = bs64_file.read() # Decode base64 decoded_img_data = base64.b64decode((bs64_data)) # Create image file from base64 with open('output.jpeg', 'wb') as img_file: img_file.write(decoded_img_data) 
  • Convert Base64 To Pdf In Python.
  • Convert Pdf to Base64 In Python.
  • Convert GPX to CSV file in python.
  • Convert IPYNB file to python file.
  • Convert Wav to mp3 format in python.
  • Convert Miles to feet in python.
  • Convert Hex To RGB In Python.
  • Convert RGB To Hex In Python.
  • Convert HTML To Docx In Python.

I hope you found what you were looking for from this python tutorial, and if you want more python tutorials like this, do join our Telegram channel to get updated.

Читайте также:  How to execute java script

Thanks for reading, have a nice day 🙂

Источник

Convert base64 string to Image in Python

Have you ever wondered how Images are being stored and transferred without being corrupted? Sometimes, when we open the images in their raw format, we observe that they are encoded in strange characters. Such characters represent Base64 string data. There is a need to convert them back to their original format. In this tutorial, we will learn how to convert Base64 string to Image in Python.

What is the Base64 module in Python?

Base64 is a module in python that is used for encoding and decoding data. A Base64 encoded data is the one wherein the binary form of data is represented in printable ASCII string format by translating to radix-64 representation. Decoding the data is exactly the opposite of encoding. Here the data in ASCII format is converted back to the binary data. This binary data is converted into byte-sized chunks which are converted back to the original format

Why Base64 encoding and decoding is required?

There are multiple reasons for converting the Base64 string to Image and vice versa. The following points explain the need for encoding and decoding images.

  1. Base64 is used to convert images into data that can be embedded within various formats such as HTML, CSS, JSON, etc. For instance, as the image data is already embedded in the document, the browser doesn’t need to make an additional web request to fetch the file. If we want to retrieve the images back from the embedded data, we can use base64 decoding.
  2. Base64 can also be used to encode the images such that they can be stored and transferred without being corrupted. Once the images have reached their destination, they can be decoded back to their original format.
Читайте также:  Css код по горизонтали

Code to convert Base64 string to Image in Python

#importing base64 module import base64 #open file with base64 string data file = open('file1.txt', 'rb') encoded_data = file.read() file.close() #decode base64 string data decoded_data=base64.b64decode((encoded_data)) #write the decoded data back to original format in file img_file = open('image.jpeg', 'wb') img_file.write(decoded_data) img_file.close()
Base64 string data stored in file1.txt:

Convert base64 string to Image in Python

Output image generated after decoding the Base64 string:

Convert base64 string to Image in Python

How does the code to convert Base64 string to Image work?

The following steps give the working of the above code to convert the base64 string to Image in Python:

  1. First, we import the base64 module
  2. Then open the file which contains base64 string data for an image. We do this using the open() function in python. The open() function takes two parameters-the file to be opened and the mode. In our case, the mode is ‘rb’ (read binary).
  3. We take the binary data and store it in a variable. Then we close the file.
  4. We decode the Base64 string data using the b64decode() function of the base64 module. This function takes the encoded data as a parameter.
  5. We create a file named image.jpeg to store the decoded data in its original Image format. To write the data onto the file, we use the write() function in Python. The function uses the mode as ‘wb’ (write binary). We then finally close the file.

Thus we have reached the end of the tutorial.
You can learn how to convert Image to Base64 string data from the following link: Base64 to Image

Источник

How to convert base64 string to image in Python?

The use of Base64 strings in applications is common, especially in encoding image data. However, when it comes to displaying images, you need to convert the Base64 string back to an image file format. In Python, this can be achieved using various methods, with the help of libraries such as Pillow and NumPy. Here are a few ways to convert a Base64 string to an image file in Python:

Method 1: Using the Pillow Library

To convert a base64 string to an image in Python using the Pillow library, follow these steps:

from PIL import Image import io import base64
base64_string = "your_base64_string_here" image_bytes = base64.b64decode(base64_string)
image_buffer = io.BytesIO(image_bytes)
image = Image.open(image_buffer)
image.save("your_image_file.jpg")

Here is the complete code:

from PIL import Image import io import base64 base64_string = "your_base64_string_here" image_bytes = base64.b64decode(base64_string) image_buffer = io.BytesIO(image_bytes) image = Image.open(image_buffer) image.save("your_image_file.jpg")

Note that you can also use the Image.show() method to display the image if you want to view it.

Method 2: Using the NumPy Library

To convert a base64 string to an image using the NumPy library in Python, you can follow these steps:

  1. Decode the base64 string into a bytes object using the base64.b64decode() method.
  2. Convert the bytes object into a NumPy array using the numpy.frombuffer() method.
  3. Reshape the NumPy array to the shape of the image using the numpy.reshape() method.
  4. Convert the NumPy array into an image using the PIL.Image.fromarray() method.
import base64 import numpy as np from PIL import Image base64_string = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACBUlEQVQ4T62TsUoDQRCGv2QjKgQYg9iY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P//y0E8DE5QvQwGkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGRkX9j8B8u4vDw8Pb3Z3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3f+2vLz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz+3/3zZ3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3f8z+Pv7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+//9t7d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3f7n4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj+7v7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+//9z3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3f8z+Pv7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+//9t7d3d3d3d3d3d3d3d3d3d3d3d3d3d3f7n4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj+7v7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+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4+Pj4+Pj4+Pj4+Pj+7v7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+//9z3d3d3d3d3d3d3d3d3d3d3d3d3d3d3f8z+Pv7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+/7+//9t7d3d3d3d3d3d3d3d3d3d3d3d3d3d3f7n4+Pj4+Pj4+Pj4+Pj4+Pj4+Pj4+P ## Method 3: Using the OpenCV Library To convert a base64 string to an image using the OpenCV library in Python, you can follow these steps: 1. Decode the base64 string into bytes using the `base64` module's `b64decode()` method. ```python import base64 base64_string = "your_base64_string_here" image_bytes = base64.b64decode(base64_string)
import numpy as np image_array = np.frombuffer(image_bytes, np.uint8)
import cv2 image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)

Now you can use the image variable to perform any further image processing or manipulation. Here’s the complete code:

import base64 import numpy as np import cv2 base64_string = "your_base64_string_here" image_bytes = base64.b64decode(base64_string) image_array = np.frombuffer(image_bytes, np.uint8) image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)

I hope this helps you convert a base64 string to an image using the OpenCV library in Python!

Источник

Оцените статью