RojaKatta commited on
Commit
43a727e
Β·
verified Β·
1 Parent(s): bde3d30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from PIL import Image
3
  import os
4
 
 
5
  def load_hairstyles():
6
  folder = "hairstyles"
7
  if not os.path.exists(folder):
@@ -13,46 +14,46 @@ def load_hairstyles():
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
 
 
2
  from PIL import Image
3
  import os
4
 
5
+ # Load all hairstyle images from folder
6
  def load_hairstyles():
7
  folder = "hairstyles"
8
  if not os.path.exists(folder):
 
14
 
15
  hairstyles = load_hairstyles()
16
 
17
+ # Automatically apply hairstyle to uploaded image
18
+ def apply_hairstyle(user_img, style_index):
19
  if user_img is None or not hairstyles:
20
  return None
21
 
22
  user_img = user_img.convert("RGBA")
23
  base_w, base_h = user_img.size
 
24
  hairstyle = hairstyles[style_index]
25
 
26
+ # Resize hairstyle to match width of head image
27
+ scale_factor = 0.9 # Adjust if needed
28
+ new_width = int(base_w * scale_factor)
29
+ new_height = int(hairstyle.height * (new_width / hairstyle.width))
30
+ resized_hairstyle = hairstyle.resize((new_width, new_height))
31
+
32
+ # Automatically center it near the top
33
+ paste_x = (base_w - new_width) // 2
34
+ paste_y = int(base_h * 0.05) # Adjust this offset if needed
35
 
 
36
  composite = Image.new("RGBA", user_img.size)
37
+ composite.paste(resized_hairstyle, (paste_x, paste_y), resized_hairstyle)
 
 
38
 
39
+ final = Image.alpha_composite(user_img, composite)
40
+ return final.convert("RGB")
 
41
 
42
+ # Gradio Interface (no sliders shown to user)
43
  with gr.Blocks() as demo:
44
+ gr.Markdown("## πŸ’‡ Salon Virtual Hairstyle Try-On (Auto Fit – No Adjustment Needed)")
45
 
46
  with gr.Row():
47
  with gr.Column():
48
+ image_input = gr.Image(type="pil", label="πŸ“· Upload Your Image")
49
  style_slider = gr.Slider(0, max(len(hairstyles)-1, 0), step=1, label="🎨 Select Hairstyle")
 
 
 
50
  apply_btn = gr.Button("✨ Apply Hairstyle")
51
  with gr.Column():
52
  result_output = gr.Image(label="πŸ” Result Preview")
53
 
54
  apply_btn.click(
55
  fn=apply_hairstyle,
56
+ inputs=[image_input, style_slider],
57
  outputs=result_output
58
  )
59