Python JPEG Image to Base64 String Example

By Hardik Savani October 30, 2023 Category : Python

Hey Dev,

This post is focused on how to convert jpeg to base64 in python. This example will help you python convert jpg image to base64. We will look at an example of how to convert jpg image to base64 string in python. I am going to show you about how to decode jpeg image to base64 string in python.

If you are looking to convert jpg 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 jpg image "image.jpg" 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.jpeg", "rb") as imageFile:
    encodedString = base64.b64encode(imageFile.read())
  
print(encodedString);

Output:

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

I hope it can help you...

Tags :
Shares