Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -92,43 +92,29 @@ def overlay_design(cloth_image, design_image):
|
|
92 |
# Ensure images are in RGBA mode
|
93 |
cloth_image = cloth_image.convert("RGBA")
|
94 |
design_image = design_image.convert("RGBA")
|
95 |
-
|
96 |
# Generate T-shirt mask
|
97 |
mask = get_tshirt_mask(cloth_image)
|
98 |
-
|
99 |
# Extract bounding box and T-shirt coordinates
|
100 |
bbox = get_bounding_box(mask)
|
101 |
tshirt_width = bbox[2] - bbox[0]
|
102 |
tshirt_height = bbox[3] - bbox[1]
|
103 |
|
104 |
-
# Resize the design to fit within the T-shirt bounding box
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
#
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
# Calculate the corners of the mask region
|
116 |
-
top_left = (x_coords.min(), y_coords.min())
|
117 |
-
top_right = (x_coords.max(), y_coords.min())
|
118 |
-
bottom_left = (x_coords.min(), y_coords.max())
|
119 |
-
bottom_right = (x_coords.max(), y_coords.max())
|
120 |
-
|
121 |
-
# Warp design image to T-shirt shape
|
122 |
-
src_points = np.float32([[0, 0], [design_image.width, 0], [0, design_image.height], [design_image.width, design_image.height]])
|
123 |
-
dst_points = np.float32([top_left, top_right, bottom_left, bottom_right])
|
124 |
-
warp_matrix = cv2.getPerspectiveTransform(src_points, dst_points)
|
125 |
-
design_warped = cv2.warpPerspective(np.array(design_image), warp_matrix, (cloth_image.width, cloth_image.height))
|
126 |
-
design_warped = Image.fromarray(design_warped).convert("RGBA")
|
127 |
-
|
128 |
-
# Mask the warped design to fit the T-shirt
|
129 |
transparent_layer = Image.new("RGBA", cloth_image.size, (0, 0, 0, 0))
|
130 |
-
transparent_layer.paste(
|
131 |
-
|
132 |
# Blend the T-shirt with the design
|
133 |
final_image = Image.alpha_composite(cloth_image, transparent_layer)
|
134 |
return final_image
|
|
|
92 |
# Ensure images are in RGBA mode
|
93 |
cloth_image = cloth_image.convert("RGBA")
|
94 |
design_image = design_image.convert("RGBA")
|
95 |
+
|
96 |
# Generate T-shirt mask
|
97 |
mask = get_tshirt_mask(cloth_image)
|
98 |
+
|
99 |
# Extract bounding box and T-shirt coordinates
|
100 |
bbox = get_bounding_box(mask)
|
101 |
tshirt_width = bbox[2] - bbox[0]
|
102 |
tshirt_height = bbox[3] - bbox[1]
|
103 |
|
104 |
+
# Resize the design to fit within 70% of the T-shirt bounding box
|
105 |
+
scale_factor = 0.7
|
106 |
+
design_width = int(tshirt_width * scale_factor)
|
107 |
+
design_height = int(tshirt_height * scale_factor)
|
108 |
+
design_image = design_image.resize((design_width, design_height), Image.ANTIALIAS)
|
109 |
+
|
110 |
+
# Calculate position to center the design on the T-shirt
|
111 |
+
x_offset = bbox[0] + (tshirt_width - design_width) // 2
|
112 |
+
y_offset = bbox[1] + (tshirt_height - design_height) // 2
|
113 |
+
|
114 |
+
# Create a transparent layer for the design
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
transparent_layer = Image.new("RGBA", cloth_image.size, (0, 0, 0, 0))
|
116 |
+
transparent_layer.paste(design_image, (x_offset, y_offset), design_image)
|
117 |
+
|
118 |
# Blend the T-shirt with the design
|
119 |
final_image = Image.alpha_composite(cloth_image, transparent_layer)
|
120 |
return final_image
|