File size: 2,076 Bytes
c4b3a12
2c9a089
 
1138828
 
ffc7966
1949160
2c9a089
 
 
 
1138828
 
 
 
 
 
 
ffc7966
1949160
 
 
 
ffc7966
1949160
2c9a089
e8176ca
2c9a089
 
9fe9ddb
 
1567106
1949160
ffc7966
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c9a089
 
 
9fe9ddb
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
50
51
52
53
import spaces
import gradio as gr
from transparent_background import Remover
from PIL import Image
import numpy as np
import io
import tempfile

@spaces.GPU
def remove_background(image):
    remover = Remover()
    if isinstance(image, Image.Image):
        output = remover.process(image)
    elif isinstance(image, np.ndarray):
        image_pil = Image.fromarray(image)
        output = remover.process(image_pil)
    else:
        raise TypeError("Unsupported image type")
    
    # 保存PNG图片到临时文件
    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
    output.save(temp_file, format="PNG")
    temp_file.close()

    return temp_file.name

# 界面设置
iface = gr.Interface(
    fn=remove_background,
    api_name=False,
    analytics_enabled=False,
    inputs=gr.Image(label="Upload Image"),
    outputs=gr.Image(label="Output Image", type="filepath"),
    title="AI Background Remover - Automatically Remove Image Backgrounds",
    description="Upload an image and our AI-powered background remover will automatically make the background transparent. Perfect for various needs requiring transparent images.",
    article="""
    ## How to Use
    1. Click **Upload Image** or drag and drop an image into the upload area.
    2. Click the **Submit** button and wait for a moment. The processed image will appear in the output area on the right.
    3. The background of the processed image will be transparent, and the image will be in PNG format for easy use.

    ### Application Scenarios
    - **E-commerce**: Remove backgrounds from product images to create clean product photos.
    - **Graphic Design**: Quickly get images with transparent backgrounds for composition and design.
    - **Social Media**: Create avatars or icons with transparent backgrounds that blend perfectly on various backgrounds.

    Our tool leverages the latest AI technology to efficiently and accurately remove image backgrounds, saving you time and effort.
    """
)

if __name__ == "__main__":
    iface.launch(show_error=True, show_api=False)