File size: 718 Bytes
40a2cd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import base64
import io
import uuid

from PIL import Image


def image_to_base64(img: Image) -> str:
    buffer = io.BytesIO()
    img.save(buffer, format="JPEG")
    img_byte_str = buffer.getvalue()
    return base64.b64encode(img_byte_str, altchars=b"-_").decode("utf-8")


def base64_to_image(base64_str: str) -> Image:
    img_data = base64.b64decode(base64_str, altchars=b"-_")
    img_buffer = io.BytesIO(img_data)
    img = Image.open(img_buffer)
    return img


def create_ids_for_images(images: list):
    """Generate ids for each image."""
    return [str(uuid.uuid5(uuid.NAMESPACE_URL, img)) for img in images]


def create_id_for_image(image: Image):
    return str(uuid.uuid5(uuid.NAMESPACE_URL, image))