Spaces:
Sleeping
Sleeping
Delete app.py
#3
by
SailajaS
- opened
app.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 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):
|
| 9 |
-
return []
|
| 10 |
-
return [
|
| 11 |
-
Image.open(os.path.join(folder, f)).convert("RGBA")
|
| 12 |
-
for f in sorted(os.listdir(folder)) if f.endswith(".png")
|
| 13 |
-
]
|
| 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 |
-
|
| 60 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|