Spaces:
Build error
Build error
Create overlay.py
Browse files- overlay.py +18 -0
overlay.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
|
| 3 |
+
def overlay_hairstyle(base_img, hairstyle_img, position):
|
| 4 |
+
base = base_img.convert("RGBA")
|
| 5 |
+
hair = hairstyle_img.convert("RGBA")
|
| 6 |
+
|
| 7 |
+
# Resize hair based on width of face area (optional tuning)
|
| 8 |
+
scale = base.width / 2.5 / hair.width
|
| 9 |
+
new_size = (int(hair.width * scale), int(hair.height * scale))
|
| 10 |
+
hair = hair.resize(new_size, Image.LANCZOS)
|
| 11 |
+
|
| 12 |
+
# Create blank canvas and paste
|
| 13 |
+
composite = Image.new("RGBA", base.size)
|
| 14 |
+
paste_x = position[0] - hair.size[0] // 2
|
| 15 |
+
paste_y = position[1] - hair.size[1] // 2
|
| 16 |
+
composite.paste(hair, (paste_x, paste_y), hair)
|
| 17 |
+
|
| 18 |
+
return Image.alpha_composite(base, composite).convert("RGB")
|