GulbaharAI commited on
Commit
0a1bfaf
·
verified ·
1 Parent(s): 27f3e4d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from rembg import remove
3
+ from PIL import Image
4
+
5
+ def replace_bg(input_img, bg_img):
6
+ # Convert input image to RGBA
7
+ input_img = input_img.convert("RGBA")
8
+
9
+ # Remove background
10
+ no_bg_image = remove(input_img)
11
+
12
+ # If no background image provided, return transparent background
13
+ if bg_img is None:
14
+ return no_bg_image
15
+
16
+ # Resize background to match foreground size
17
+ bg_img = bg_img.convert("RGBA").resize(no_bg_image.size)
18
+
19
+ # Combine foreground and background
20
+ result_image = Image.alpha_composite(bg_img, no_bg_image)
21
+ return result_image
22
+
23
+ # Create the Gradio interface
24
+ demo = gr.Interface(
25
+ fn=replace_bg,
26
+ inputs=[
27
+ gr.Image(type="pil", label="📷 Upload Your Image"),
28
+ gr.Image(type="pil", label="🎨 Upload Background Image (Optional)")
29
+ ],
30
+ outputs=gr.Image(type="pil", label="✨ Final Result"),
31
+ title="🎨 Professional Background Replacer",
32
+ description="Upload any image and replace its background with a new one. Leave background empty for transparent result."
33
+ )
34
+
35
+ # Launch the application
36
+ if __name__ == "__main__":
37
+ demo.launch()