gaur3009 commited on
Commit
068132d
·
verified ·
1 Parent(s): 8e12787

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -20
app.py CHANGED
@@ -96,31 +96,41 @@ def overlay_design(cloth_image, design_image):
96
  # Generate T-shirt mask
97
  mask = get_tshirt_mask(cloth_image)
98
 
99
- # Extract bounding box for precise placement
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 the T-shirt
105
- design_width = int(tshirt_width * 0.6)
106
- design_height = int(tshirt_height * 0.4) # Make the design height smaller for a better fit
107
- resized_design = design_image.resize((design_width, design_height))
108
-
109
- # Adjust position to move the design closer to the chest area
110
- design_position = (
111
- bbox[0] + (tshirt_width - design_width) // 2,
112
- bbox[1] + int(tshirt_height * 0.25) # Slightly lower the design
113
- )
114
-
115
- # Create a transparent layer for the design
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  transparent_layer = Image.new("RGBA", cloth_image.size, (0, 0, 0, 0))
117
- transparent_layer.paste(resized_design, design_position, resized_design)
118
-
119
- # Mask the design to the T-shirt area
120
- masked_design = Image.composite(transparent_layer, Image.new("RGBA", cloth_image.size), mask)
121
-
122
- # Combine the cloth image with the masked design
123
- final_image = Image.alpha_composite(cloth_image, masked_design)
124
  return final_image
125
 
126
  def debug_intermediate_outputs(cloth_image, mask):
 
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
+ design_image = design_image.resize((tshirt_width, tshirt_height))
106
+
107
+ # Convert mask to NumPy array for warping
108
+ mask_np = np.array(mask)
109
+
110
+ # Identify T-shirt edges for perspective warp
111
+ y_coords, x_coords = np.where(mask_np > 0)
112
+ if len(y_coords) < 4 or len(x_coords) < 4:
113
+ raise Exception("Insufficient mask data for warping.")
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(design_warped, (0, 0), mask)
131
+
132
+ # Blend the T-shirt with the design
133
+ final_image = Image.alpha_composite(cloth_image, transparent_layer)
 
 
 
134
  return final_image
135
 
136
  def debug_intermediate_outputs(cloth_image, mask):