File size: 657 Bytes
7134ce7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from PIL import Image
import io
import base64

def pil2base64(pil_image: Image.Image) -> str:
    """

    Converts a PIL image object to a base64-encoded string.



    Parameters:

    pil_image (PIL.Image.Image): The image to be converted to base64.



    Returns:

    str: The base64-encoded string representation of the image.

    """
    try:
        binary_stream = io.BytesIO()
        pil_image.save(binary_stream, format="PNG")
        binary_data = binary_stream.getvalue()
        return base64.b64encode(binary_data).decode('utf-8')
    except Exception as e:
        raise RuntimeError(f"Failed to convert image to base64: {e}")