Spaces:
Runtime error
Runtime error
File size: 1,246 Bytes
a135247 13ba839 a135247 4470c14 a4b35a8 4e13f77 13ba839 4e13f77 13ba839 a135247 4e13f77 a135247 13ba839 80f97ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
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
|