Image To Base64 Conversion In Python

Python provides several modules to work with images and encode them in base64 format. One of the most commonly used modules is the Pillow module, an updated version of the Python Imaging Library .To convert an image to base64 format in Python.

import base64
def img_to_bs64(image_path):
    with open(img_path, "rb") as img_file:
        return base64.b64encode(img_file.read()).decode('utf-8')
img_path = "example_image.jpg"  
base64_str = img_to_bs64(img_path)
print(base64_str[:100])

 

Leave a Comment

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

Scroll to Top