WeVi commited on
Commit
27309aa
·
verified ·
1 Parent(s): b44e72b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -21
app.py CHANGED
@@ -1,28 +1,64 @@
1
  import gradio as gr
2
- from PIL import Image
3
  from rembg import remove
 
4
  import io
5
 
6
- def remove_background(input_image):
7
- # Ensure image is RGBA for transparency
8
- input_image = input_image.convert("RGBA")
9
-
10
- # Remove background
11
- result = remove(input_image)
12
-
13
- return Image.open(io.BytesIO(result))
14
-
15
- with gr.Blocks(title="AI Background Remover (rembg)") as demo:
16
- gr.Markdown("🧠 **AI Background Remover** - Powered by `rembg` for transparent results")
17
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  with gr.Row():
19
- with gr.Column():
20
- input_image = gr.Image(label="Upload Image", type="pil")
21
- submit = gr.Button("Remove Background")
22
-
23
- with gr.Column():
24
- output_image = gr.Image(label="Transparent PNG Output")
25
-
26
- submit.click(fn=remove_background, inputs=input_image, outputs=output_image)
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  demo.launch()
 
1
  import gradio as gr
 
2
  from rembg import remove
3
+ from PIL import Image
4
  import io
5
 
6
+ # Preserve original resolution & optimize for high-res images
7
+ def process_image(image, background_type, bg_color, bg_image):
8
+ try:
9
+ input_image = Image.open(image).convert("RGBA")
10
+ # Use rembg with bytes instead of loading into RAM completely
11
+ buffered = io.BytesIO()
12
+ input_image.save(buffered, format="PNG")
13
+ result = remove(buffered.getvalue())
14
+ output = Image.open(io.BytesIO(result)).convert("RGBA")
15
+
16
+ if background_type == "Solid Color":
17
+ bg = Image.new("RGBA", output.size, bg_color)
18
+ bg.paste(output, (0, 0), output)
19
+ return bg
20
+
21
+ elif background_type == "Custom Image" and bg_image:
22
+ custom_bg = Image.open(bg_image).convert("RGBA").resize(output.size)
23
+ custom_bg.paste(output, (0, 0), output)
24
+ return custom_bg
25
+
26
+ else:
27
+ return output
28
+ except Exception as e:
29
+ return f"❌ Error: {str(e)}"
30
+
31
+ # For multiple images
32
+ def batch_process(images, background_type, bg_color, bg_image):
33
+ return [process_image(img, background_type, bg_color, bg_image) for img in images]
34
+
35
+ # Gradio UI
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown("## 🎯 High-Resolution AI Background Remover with Custom Background")
38
+
39
+ with gr.Row():
40
+ images_input = gr.File(label="📤 Upload High-Res Image(s)", file_types=["image"], file_count="multiple")
41
+
42
  with gr.Row():
43
+ background_type = gr.Radio(
44
+ ["Transparent", "Solid Color", "Custom Image"],
45
+ label="Choose Background Type",
46
+ value="Transparent"
47
+ )
48
+ bg_color = gr.ColorPicker(label="Solid Color", value="#ffffff", visible=False)
49
+ bg_image = gr.File(label="Custom Background Image", file_types=["image"], visible=False)
50
+
51
+ output_gallery = gr.Gallery(label="🖼️ Output Images", columns=2, height=600)
52
+
53
+ def toggle_visibility(bg_type):
54
+ return (
55
+ gr.update(visible=(bg_type == "Solid Color")),
56
+ gr.update(visible=(bg_type == "Custom Image"))
57
+ )
58
+
59
+ background_type.change(toggle_visibility, inputs=background_type, outputs=[bg_color, bg_image])
60
+
61
+ run_btn = gr.Button("🚀 Start Processing")
62
+ run_btn.click(fn=batch_process, inputs=[images_input, background_type, bg_color, bg_image], outputs=output_gallery)
63
 
64
  demo.launch()