Download scripts/utlis.py from BechusRantus/injected_thinking: direct link, hf CLI and curl.
- Browser
- Download file 657 Bytes
-
https://huggingface.co/BechusRantus/injected_thinking/resolve/main/scripts/utlis.py
- Command line
-
hf download hf://BechusRantus/injected_thinking/scripts/utlis.py
-
curl -L -o utlis.py https://huggingface.co/BechusRantus/injected_thinking/resolve/main/scripts/utlis.py
657 Bytes
| 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}") |