Spaces:
Sleeping
Sleeping
Download app.py from user-agent/image_to_base64: direct link, hf CLI and curl.
- Browser
- Download file 630 Bytes
-
https://huggingface.co/spaces/user-agent/image_to_base64/resolve/main/app.py
- Command line
-
hf download hf://spaces/user-agent/image_to_base64/app.py
-
curl -L -o app.py https://huggingface.co/spaces/user-agent/image_to_base64/resolve/main/app.py
630 Bytes
| import gradio as gr | |
| import base64 | |
| from PIL import Image | |
| import io | |
| def image_to_base64(image): | |
| # Convert PIL Image to bytes | |
| buffered = io.BytesIO() | |
| image.save(buffered, format="JPEG") | |
| # Encode bytes to Base64 string | |
| img_str = base64.b64encode(buffered.getvalue()).decode() | |
| return img_str | |
| # Define the Gradio interface using the updated syntax | |
| iface = gr.Interface( | |
| fn=image_to_base64, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Textbox(), | |
| title="Image to Base64 Encoder", | |
| description="Upload an image and convert it to a Base64 encoded string." | |
| ) | |
| # Launch the interface | |
| iface.launch() | |