In this post, we will learn how to convert image to base64 in Python with a detailed explanation and example.
But before we jump into the programming part, let’s first understand what base64 is and why we need to convert images to base64.
What is Base64?
Base64 is an encoding method that encodes binary data into a string of text composed of numbers, letters, and special symbols.
Why do we need to convert images to Base64?
Base64 is commonly used in web development to embed images directly in code, instead of linking to external image files. It is also frequently used in APIs when images need to be sent as part of the response in text-based formats like JSON or XML.
While Base64 encoding increases the size of the data by about 33%, it enables binary data (such as images) to be transmitted in a text-friendly format. This is especially useful for APIs or situations where the image must be embedded directly in the response, without external references. Developers use Base64 when embedding images directly into responses or code is preferred over managing separate files.
Now I hope you understand what Base64 is and why we need to use it. If you’re interested in testing how to encode an image to Base64, you can try using this online tool 👉 online Image to Base64 convertor.
Now let’s jump into coding part…
Steps to Convert Image to Base64 in Python
Converting an image to base64 in Python is very simple. You just need to import Python’s built-in module, base64
, which provides a function that will help us convert the image to base64. Below is a step-by-step guide on how you convert an image to base64 in Python.
Step 1: Import necessary module
import base64
Step 2: Open your image file in binary format
with open("your_image.jpg", "rb") as image_file:
image_data = image_file.read()
Step 3: Encode your image data into base64 using base64.b64encode()
function.
base64_encoded = base64.b64encode(image_data)
Step 4: Convert your base64 encoded bytes into a string.
base64_string = base64_encoded.decode("utf-8")
Step 5: Print or save your Base64 string
print(base64_string)
# if you want to save Base64 string in .txt
# with open("base64.txt", "w") as b64:
# b64.write(base64_string)
Complete code:
# Step 1: Import necessary module
import base64
# Step 2: Open your image file in binary format
with open("demo_image.png", "rb") as image_file:
image_data = image_file.read()
# Step 3: Encode your image data into base64 using base64.b64encode() function.
base64_encoded = base64.b64encode(image_data)
# Step 4: Convert your base64 encoded bytes into a string.
base64_string = base64_encoded.decode("utf-8")
# Step 5: Print or save your Base64 string
print(base64_string)
# if you want to save your base64 string in .txt then do:
# with open("base64.txt", "w") as b64:
# b64.write(base64_string)
Output:
iVBORw0KGgoAAAANSUhEUgAAAUoAAAFKCAIAAAD0S4F...
This is all about how we can convert an image to base64 in Python. I hope this article was helpful to you. Thank you for reading, and I’ll see you in the next one.