File size: 1,070 Bytes
9404436
 
 
 
fc3c784
 
 
 
 
9404436
 
 
fc3c784
9404436
fc3c784
9404436
 
fc3c784
 
 
 
9404436
fc3c784
 
9404436
 
 
 
09c3c4a
 
 
 
 
fc3c784
 
 
 
9404436
fc3c784
9404436
 
 
62cb49a
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
import gradio as gr
from rembg import remove
from PIL import Image
import io
import uuid
import os

OUTPUT_DIR = "outputs"
os.makedirs(OUTPUT_DIR, exist_ok=True)

def remove_background(image: Image.Image):
    if image is None:
        return None, None

    # Rimuove lo sfondo
    output = remove(image)

    # Salva l'immagine in un file temporaneo
    file_id = str(uuid.uuid4())
    output_path = os.path.join(OUTPUT_DIR, f"{file_id}.png")
    output.save(output_path)

    # Restituisce: immagine modificata, link file per download
    return output, output_path

# Interfaccia Gradio
demo = gr.Interface(
    fn=remove_background,
    inputs=gr.Image(
    type="pil",
    label="Carica un'immagine o usa la webcam",
    sources=["upload", "webcam"],
    ),
    outputs=[
        gr.Image(type="pil", label="Immagine senza sfondo"),
        gr.File(label="Scarica il file PNG")
    ],
    title="Rimozione Sfondo da Immagine",
    description="Carica un'immagine, rimuovi lo sfondo e scarica il file PNG risultante."
)

if __name__ == "__main__":
    demo.launch()