Danzer93 commited on
Commit
0f1a53b
·
verified ·
1 Parent(s): f154d1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -18
app.py CHANGED
@@ -5,39 +5,56 @@ import io
5
  import uuid
6
  import os
7
 
 
8
  OUTPUT_DIR = "outputs"
9
  os.makedirs(OUTPUT_DIR, exist_ok=True)
10
 
 
11
  def remove_background(image: Image.Image):
12
  if image is None:
13
  return None, None
14
 
15
- # Rimuove lo sfondo
16
  output = remove(image)
17
-
18
- # Salva l'immagine in un file temporaneo
19
  file_id = str(uuid.uuid4())
20
  output_path = os.path.join(OUTPUT_DIR, f"{file_id}.png")
21
  output.save(output_path)
22
 
23
- # Restituisce: immagine modificata, link file per download
24
  return output, output_path
25
 
26
- # Interfaccia Gradio
27
- demo = gr.Interface(
28
- fn=remove_background,
29
- inputs=gr.Image(
30
- type="pil",
31
- label="Carica un'immagine o usa la webcam",
32
- sources=["upload", "webcam"],
33
- ),
34
- outputs=[
35
- gr.Image(type="pil", label="Immagine senza sfondo"),
36
- gr.File(label="Scarica il file PNG")
37
- ],
38
- title="Rimozione Sfondo da Immagine",
39
- description="Carica un'immagine, rimuovi lo sfondo e scarica il file PNG risultante."
40
  )
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  if __name__ == "__main__":
43
  demo.launch()
 
5
  import uuid
6
  import os
7
 
8
+ # Cartella per output
9
  OUTPUT_DIR = "outputs"
10
  os.makedirs(OUTPUT_DIR, exist_ok=True)
11
 
12
+ # Funzione principale
13
  def remove_background(image: Image.Image):
14
  if image is None:
15
  return None, None
16
 
 
17
  output = remove(image)
 
 
18
  file_id = str(uuid.uuid4())
19
  output_path = os.path.join(OUTPUT_DIR, f"{file_id}.png")
20
  output.save(output_path)
21
 
 
22
  return output, output_path
23
 
24
+ # Immagini della galleria (metti i file nella cartella "gallery")
25
+ gallery_images = [f"gallery/{img}" for img in sorted(os.listdir("gallery")) if img.endswith((".png", ".jpg", ".jpeg"))][:5]
26
+
27
+ # Funzione per usare immagine dalla galleria
28
+ def load_gallery_image(image_path):
29
+ return Image.open(image_path)
30
+
31
+ # Tema localizzato in italiano (solo per gradio >=4.15)
32
+ italian_ui = gr.themes.Base(
33
+ language="it"
 
 
 
 
34
  )
35
 
36
+ with gr.Blocks(theme=italian_ui, title="Rimozione Sfondo") as demo:
37
+ gr.Markdown("# ✂️ Rimozione Sfondo da Immagine")
38
+ gr.Markdown("Carica una tua immagine, usane una dalla webcam oppure scegli dalla galleria.")
39
+
40
+ with gr.Row():
41
+ image_input = gr.Image(
42
+ type="pil",
43
+ label="Carica immagine",
44
+ sources=["upload", "webcam"],
45
+ )
46
+ gallery = gr.Gallery(label="Galleria di esempio").style(grid=5, height="auto")
47
+ gallery.value = gallery_images
48
+
49
+ output_image = gr.Image(type="pil", label="Risultato")
50
+ output_file = gr.File(label="Scarica PNG")
51
+
52
+ # Quando si seleziona un'immagine dalla galleria
53
+ gallery.select(fn=load_gallery_image, outputs=image_input)
54
+
55
+ # Bottone per elaborare
56
+ btn = gr.Button("Rimuovi Sfondo")
57
+ btn.click(fn=remove_background, inputs=image_input, outputs=[output_image, output_file])
58
+
59
  if __name__ == "__main__":
60
  demo.launch()