File size: 1,342 Bytes
997f1b0
95234b5
997f1b0
95234b5
997f1b0
95234b5
997f1b0
95234b5
997f1b0
95234b5
 
3aa0fcb
95234b5
3aa0fcb
997f1b0
95234b5
3aa0fcb
 
 
 
 
 
 
 
 
 
95234b5
997f1b0
 
 
 
 
 
 
95234b5
 
3aa0fcb
 
 
 
 
 
 
 
 
95234b5
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()