RojaKatta commited on
Commit
2265de2
Β·
verified Β·
1 Parent(s): a0c8f7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -2,36 +2,58 @@ import gradio as gr
2
  from PIL import Image
3
  import os
4
 
5
- # Load hairstyles
6
  def load_hairstyles():
7
  folder = "hairstyles"
8
- return [Image.open(os.path.join(folder, f)).convert("RGBA") for f in os.listdir(folder) if f.endswith(".png")]
 
 
 
 
 
9
 
10
  hairstyles = load_hairstyles()
11
 
12
- # Overlay hairstyle
13
- def apply_hairstyle(user_img, style_index):
14
- if user_img is None:
15
  return None
16
-
17
  user_img = user_img.convert("RGBA")
18
- hairstyle = hairstyles[style_index].resize(user_img.size)
19
-
20
- result = Image.alpha_composite(user_img, hairstyle)
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  return result.convert("RGB")
22
 
23
- # Build Gradio UI
24
  with gr.Blocks() as demo:
25
- gr.Markdown("## πŸ’‡ Salon Virtual Hairstyle Try-On")
26
-
27
  with gr.Row():
28
  with gr.Column():
29
- image_input = gr.Image(source="upload", type="pil", label="Upload or Capture Image")
30
- style_slider = gr.Slider(0, len(hairstyles)-1, step=1, label="Select Hairstyle")
31
- submit_btn = gr.Button("Apply Hairstyle")
 
 
 
32
  with gr.Column():
33
- result_img = gr.Image(label="Result Preview")
34
 
35
- submit_btn.click(fn=apply_hairstyle, inputs=[image_input, style_slider], outputs=result_img)
 
 
 
 
36
 
37
  demo.launch()
 
2
  from PIL import Image
3
  import os
4
 
 
5
  def load_hairstyles():
6
  folder = "hairstyles"
7
+ if not os.path.exists(folder):
8
+ return []
9
+ return [
10
+ Image.open(os.path.join(folder, f)).convert("RGBA")
11
+ for f in sorted(os.listdir(folder)) if f.endswith(".png")
12
+ ]
13
 
14
  hairstyles = load_hairstyles()
15
 
16
+ def apply_hairstyle(user_img, style_index, x_offset, y_offset, scale):
17
+ if user_img is None or not hairstyles:
 
18
  return None
19
+
20
  user_img = user_img.convert("RGBA")
21
+ base_w, base_h = user_img.size
22
+
23
+ hairstyle = hairstyles[style_index]
24
+
25
+ # Resize the hairstyle based on scale
26
+ new_size = (int(base_w * scale), int(hairstyle.height * (base_w * scale / hairstyle.width)))
27
+ hairstyle = hairstyle.resize(new_size)
28
+
29
+ # Create a blank transparent image to position the hairstyle
30
+ composite = Image.new("RGBA", user_img.size)
31
+ paste_x = int((base_w - new_size[0]) / 2 + x_offset)
32
+ paste_y = int(y_offset)
33
+ composite.paste(hairstyle, (paste_x, paste_y), hairstyle)
34
+
35
+ # Overlay it
36
+ result = Image.alpha_composite(user_img, composite)
37
  return result.convert("RGB")
38
 
 
39
  with gr.Blocks() as demo:
40
+ gr.Markdown("## πŸ’‡ Salon Virtual Hairstyle Try-On (Adjustable)")
41
+
42
  with gr.Row():
43
  with gr.Column():
44
+ image_input = gr.Image(type="pil", label="πŸ“· Upload an Image")
45
+ style_slider = gr.Slider(0, max(len(hairstyles)-1, 0), step=1, label="🎨 Select Hairstyle")
46
+ x_offset = gr.Slider(-200, 200, value=0, step=1, label="β¬…οΈβž‘οΈ Move Left / Right")
47
+ y_offset = gr.Slider(-200, 200, value=0, step=1, label="⬆️⬇️ Move Up / Down")
48
+ scale = gr.Slider(0.3, 2.0, value=1.0, step=0.05, label="πŸ“ Scale Hairstyle")
49
+ apply_btn = gr.Button("✨ Apply Hairstyle")
50
  with gr.Column():
51
+ result_output = gr.Image(label="πŸ” Result Preview")
52
 
53
+ apply_btn.click(
54
+ fn=apply_hairstyle,
55
+ inputs=[image_input, style_slider, x_offset, y_offset, scale],
56
+ outputs=result_output
57
+ )
58
 
59
  demo.launch()