image_to_base64 / app.py
user-agent's picture
Create app.py
1576a7f verified
raw
history blame
605 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
iface = gr.Interface(
fn=image_to_base64,
inputs=gr.inputs.Image(type="pil"),
outputs="text",
title="Image to Base64 Encoder",
description="Upload an image and convert it to a Base64 encoded string."
)
# Launch the interface
iface.launch()