Spaces:
Runtime error
Runtime error
import gradio as gr | |
from rembg import remove | |
from PIL import Image | |
import base64 | |
def remove_background(input_image): | |
input = Image.open(input_image) # load image | |
output = remove(input) # remove background | |
return output | |
input_image = gr.inputs.Image(type="filepath", label="Input Image") | |
output_image = gr.outputs.Image(type="filepath", label="Output Image") | |
# Create a Gradio interface | |
iface = gr.Interface(remove_background, | |
inputs=input_image, | |
outputs=output_image, | |
title='Image Background Remover', | |
description='Upload an image and it will remove.') | |
# Define a function to convert PIL image to base64 string | |
def pil_to_base64_string(pil_image): | |
buffered = BytesIO() | |
pil_image.save(buffered, format="PNG") | |
img_str = base64.b64encode(buffered.getvalue()).decode() | |
return img_str | |
# Launch the interface | |
iface.launch() | |
output_pil = iface.get_output() # get the output PIL image from the interface | |
output_base64_str = pil_to_base64_string(output_pil) # convert the PIL image to base64 string | |
with open("output_image.png", "wb") as f: | |
f.write(base64.b64decode(output_base64_str)) # write the decoded base64 string to a file | |