rembg / app.py
nolenfelten's picture
Update app.py
997f1b0 verified
print("import gradio as gr")
import gradio as gr
print("from rembg import remove")
from rembg import remove
print("from PIL import Image")
from PIL import Image
print("import numpy as np")
import numpy as np
print("import io")
import io
def remove_bg(image, format, threshold):
input_image = Image.fromarray(np.array(image))
# Remove background with threshold adjustment (if applicable)
output_image = remove(input_image)
# Convert to desired format
buffer = io.BytesIO()
if format == "PNG":
output_image.save(buffer, format="PNG")
elif format == "JPEG":
output_image.save(buffer, format="JPEG")
buffer.seek(0)
return buffer
def convert_pil_to_np(image):
return np.array(image)
def convert_np_to_pil(image):
return Image.fromarray(image)
print("Initialize Interface")
iface = gr.Interface(
fn=remove_bg,
inputs=[
gr.Image(type="pil", label="Input Image"),
gr.Radio(choices=["PNG", "JPEG"], label="Output Format", value="PNG"),
gr.Slider(0, 100, step=1, label="Threshold", value=0)
],
outputs=gr.Image(type="file", label="Output Image"),
title="Enhanced Background Remover",
description="Upload an image to remove the background. Adjust the output format and sensitivity.",
layout="horizontal"
)
iface.launch()