Download utils.py from Idhammz/Chat-AI: direct link, hf CLI and curl.
- Browser
- Download file 490 Bytes
-
https://huggingface.co/spaces/Idhammz/Chat-AI/resolve/main/utils.py
- Command line
-
hf download hf://spaces/Idhammz/Chat-AI/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Idhammz/Chat-AI/resolve/main/utils.py
490 Bytes
| import re | |
| import base64 | |
| from PIL import Image | |
| from io import BytesIO | |
| def base64_to_pil(img_base64): | |
| image_data = re.sub('data:image/.+;base64,', '', str(img_base64)) | |
| pil_image = Image.open(BytesIO(base64.b64decode(image_data))) | |
| return pil_image | |
| def np_to_base64(img_np): | |
| img = Image.fromarray(img_np.astype('uint8'), 'RGB') | |
| buffered = BytesIO() | |
| img.save(buffered, format="PNG") | |
| return u"data:image/png;base64," + base64.b64encode(buffered.getvalue()).decode() | |