fastSD / backend /base64_image.py
thejagstudio's picture
Upload 61 files
510ee71 verified
raw
history blame contribute delete
514 Bytes
from io import BytesIO
from base64 import b64encode, b64decode
from PIL import Image
def pil_image_to_base64_str(
image: Image,
format: str = "JPEG",
) -> str:
buffer = BytesIO()
image.save(buffer, format=format)
buffer.seek(0)
img_base64 = b64encode(buffer.getvalue()).decode("utf-8")
return img_base64
def base64_image_to_pil(base64_str) -> Image:
image_data = b64decode(base64_str)
image_buffer = BytesIO(image_data)
image = Image.open(image_buffer)
return image