ItSolutionStuff.com

Python PNG Image to Base64 String Example

By Hardik Savani • October 30, 2023
Python

Hi Artisan,

In this tutorial, you will discover how to convert png to base64 in python. We will look at an example of python convert png image to base64. This article will give you a simple example of how to convert png image to base64 string in python. We will look at an example of how to decode png image to base64 string in python.

If you are looking to convert png image to base64 string in python then I will help you with that. I will give you a very simple example, we will take one png image "image.png" and then convert it into the base64 string. we will use base64 library. So, without further ado, let's see simple examples: You can use these examples with python3 (Python 3) version.

Example:

main.py

# Import base64 Module
import base64
   
# Python Convert Image to Base64 String
with open("image.png", "rb") as imageFile:
    encodedString = base64.b64encode(imageFile.read())
  
print(encodedString);

Output:

JVBERi0xLjMNCiXi48/TDQoNCjEgMCBvYmoNCjw8DQovVHlwZSAvQ
NCjw8DQovVHlwZSAvQ2F0YWxvZw0KL091dGxpbmVzIDIgM
48/TDQoNCjEgMCBvYmoNCjw8DQovVHlwZSAvQ2F0YWxvZw0KL091dGxpbmV
QoNCjEgMCBvYmoNCjw8DQ......

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Python Convert base64 String to PNG Image Example

Read Now →

How to Convert base64 to PDF in Python?

Read Now →

How to Convert base64 to Image in Python?

Read Now →

How to Get First Value in Dictionary Python?

Read Now →

Python Dictionary Delete Item by Key Example

Read Now →

How to Add Element in Dictionary Python?

Read Now →

Python Read Excel File with Multiple Sheets Example

Read Now →

Python Write Excel File with Multiple Sheets Example

Read Now →

How to Write a JSON File in Python?

Read Now →

Python Read CSV File Without Header Example

Read Now →

Python Create Zip Archive from Directory Example

Read Now →

How to Check if Today is Saturday or not in Python?

Read Now →