Using 'base64.b64encode' method, we can convert the image to base64 encoded string.
Step 1: Open the image file in binary format.
with open(image_path, "rb") as img_file
Step 2: Get base64 string from the image content using ‘base64.b64encode’ method.
base64.b64encode(img_file.read())
convert_image_to_base64.py
import base64
def get_base64(image_path):
with open(image_path, "rb") as img_file:
return base64.b64encode(img_file.read())
image_path = '/Users/krishna/Desktop/1.png'
binary_string = get_base64(image_path)
print('binary_string -> ', binary_string)
No comments:
Post a Comment