Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert Image to Base64 String and Vice Versa in Python

  • 13 Oct, 2025
  • Com 0
Converting an image to base64 in Python

When you convert an image to a Base64 string, you are encoding binary image data into a text-based format using 64 printable characters (A-Z, a-z, 0-9, +, /).

Here’s a step-by-step guide:

  1. Step 1: Import the Base64 Module
  2. Step 2: Open the image file in binary read mode (‘rb’).
  3. Step 3: Use the base64.b64encode() function to encode the read bytes.

Here is the image that will be used in this code:

basic input image to convert to base64

Here is the code:

import base64


def image_to_base64(file_path):
    with open(file_path, 'rb') as image_file:
        image_bytes = image_file.read()
        base64_string = base64.b64encode(image_bytes).decode('utf-8')
    return base64_string


# Example usage
base64_str = image_to_base64('Tower.png')
print(base64_str[:100] + '...')  # Truncated for brevity
# Output: iVBORw0KGgoAAAANSUhEUgAABFQAAAXyCAYAAAAxz74XAAAQAElEQVR4Aez9CdRuWXrXh/3/z36/W92Seqquqee5q6q7epQwdrwc...

with open("encoded_image.txt", "w") as text_file:
    text_file.write(base64_str)

In the output, we truncated the string for brevity and wrote the encoded text in a .txt file. But why do we need to store the encoded text? We may want to read that data and convert it back to an image.

Here is the encoded_image.txt file as an output:

screenshot of encoded_txt file

Converting Base64 String to Image

Here’s a step-by-step guide:

  1. Import the Base64 Module.
  2. Read the Base64 Encoded File.
  3. Decode the file using base64.b64decode() function.
  4. Write the decoded data to a new image file.

Here is the encoded_image.txt file:

encoded_txt file

We will read this file and convert it into an image.

import base64

with open("encoded_image.txt", "r") as text_file:
    base64_data = text_file.read()

image_data = base64.b64decode(base64_data)

with open("decoded_tower.jpg", "wb") as image_file:
    image_file.write(image_data)

Here is the output image:

decoded_tower

You can see that we restored the image for viewing or further processing. This is a complete vice-versa.

Post Views: 29
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert Set to String in Python
How to Convert List to String in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend