mocktestgen commited on
Commit
720c195
·
verified ·
1 Parent(s): df7184c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -28
app.py CHANGED
@@ -1,43 +1,53 @@
1
  import gradio as gr
2
  from PIL import Image
 
3
 
4
  def overlay_image(background, overlay):
5
- # Resize overlay to fit the background
6
- overlay = overlay.resize(background.size, Image.ANTIALIAS)
7
-
8
- # Create a new image for the result
9
- result = Image.new("RGBA", background.size)
10
-
11
- # Paste the background and overlay
12
- result.paste(background, (0, 0))
13
- result.paste(overlay, (0, 0), overlay)
14
-
15
- return result
 
 
16
 
17
  def virtual_try_on(user_image, shirt_image):
18
- # Convert images to RGBA
19
- user_image = user_image.convert("RGBA")
20
- shirt_image = Image.open(shirt_image).convert("RGBA")
21
-
22
- # Overlay the shirt on the user image
23
- result_image = overlay_image(user_image, shirt_image)
24
-
25
- return user_image, result_image
 
 
 
 
 
 
26
 
27
- # Gradio interface
28
  iface = gr.Interface(
29
  fn=virtual_try_on,
30
  inputs=[
31
- gr.Image(type="pil", label="Upload your front-facing photo"),
32
- gr.Image(type="filepath", label="Upload or choose a t-shirt design (PNG format)")
33
  ],
34
  outputs=[
35
- gr.Image(label="Original Photo"),
36
- gr.Image(label="Try-On Preview")
37
  ],
38
- title="Virtual Try-On Tool",
39
- description="Try on our t-shirts by uploading your photo and selecting a t-shirt design.",
40
- theme="default"
 
 
41
  )
42
 
43
- iface.launch()
 
1
  import gradio as gr
2
  from PIL import Image
3
+ import numpy as np
4
 
5
  def overlay_image(background, overlay):
6
+ try:
7
+ # Ensure both images have alpha channel
8
+ background = background.convert("RGBA")
9
+ overlay = overlay.convert("RGBA")
10
+
11
+ # Resize overlay to match background
12
+ overlay = overlay.resize(background.size, Image.Resampling.LANCZOS)
13
+
14
+ # Composite images
15
+ return Image.alpha_composite(background, overlay)
16
+ except Exception as e:
17
+ print(f"Error in overlay: {str(e)}")
18
+ return None
19
 
20
  def virtual_try_on(user_image, shirt_image):
21
+ try:
22
+ # Open and verify images
23
+ user_img = Image.fromarray(user_image) if isinstance(user_image, np.ndarray) else user_image
24
+ shirt_img = Image.open(shirt_image) if isinstance(shirt_image, str) else shirt_image
25
+
26
+ result = overlay_image(user_img.convert("RGBA"), shirt_img.convert("RGBA"))
27
+
28
+ if result is None:
29
+ raise gr.Error("Failed to process images. Please check your inputs.")
30
+
31
+ return [user_img, result]
32
+ except Exception as e:
33
+ print(f"Error in try-on: {str(e)}")
34
+ raise gr.Error(f"Processing error: {str(e)}")
35
 
 
36
  iface = gr.Interface(
37
  fn=virtual_try_on,
38
  inputs=[
39
+ gr.Image(label="Your Photo", type="pil"),
40
+ gr.Image(label="T-Shirt Design", type="filepath")
41
  ],
42
  outputs=[
43
+ gr.Image(label="Original"),
44
+ gr.Image(label="Virtual Try-On")
45
  ],
46
+ title="YOURBRAND Virtual Try-On",
47
+ description="Upload your photo and a t-shirt design to see how it looks!",
48
+ examples=[
49
+ ["sample_person.jpg", "sample_shirt.png"]
50
+ ]
51
  )
52
 
53
+ iface.launch(debug=True)