cszhzleo's picture
Create utils.py
1c873bf verified
raw
history blame contribute delete
788 Bytes
import base64
from io import BytesIO
from PIL import Image
def convert_to_base64(image_file_path):
"""
Convert PIL images to Base64 encoded strings
:param pil_image: PIL image
:return: Re-sized Base64 string
"""
pil_image = Image.open(image_file_path)
buffered = BytesIO()
pil_image.save(buffered, format="png") # You can change the format if needed
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return img_str
def convert_to_html(img_base64):
"""
Disply base64 encoded string as image
:param img_base64: Base64 string
"""
# Create an HTML img tag with the base64 string as the source
image_html = f'<img src="data:image/jpeg;base64,{img_base64}" style="max-width: 100%;"/>'
return image_html