Spaces:
Runtime error
Runtime error
Download utils.py from dmedhi/image-search-engine: direct link, hf CLI and curl.
- Browser
- Download file 718 Bytes
-
https://huggingface.co/spaces/dmedhi/image-search-engine/resolve/main/utils.py
- Command line
-
hf download hf://spaces/dmedhi/image-search-engine/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/dmedhi/image-search-engine/resolve/main/utils.py
718 Bytes
| 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)) | |