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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -29
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
- 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
 
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