Spaces:
Running
Running
File size: 19,593 Bytes
d0a37b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | """
2D Sewing Pattern Generator from Garment Analysis
Generates flat 2D sewing pattern pieces based on garment parameters.
Each garment type has a parametric pattern model that produces
pattern pieces with proper seam allowances, grain lines, and notches.
"""
import math
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.patches import FancyArrowPatch
from matplotlib.path import Path as MPath
from io import BytesIO
from PIL import Image
from typing import Dict, List, Tuple, Optional
# Color palette for pattern pieces
PIECE_COLORS = [
'#FFB3BA', '#BAFFC9', '#BAE1FF', '#FFFFBA', '#E8BAFF',
'#FFD4BA', '#BAF0FF', '#FFBAE8', '#D4FFBA', '#BAC8FF'
]
def generate_bodice_front(params: Dict) -> Dict:
"""Generate front bodice pattern piece."""
bust = params.get('bust', 90) / 4 + 2
waist = params.get('waist', 70) / 4 + 2
shoulder = params.get('shoulder_width', 40) / 2
bodice_length = params.get('bodice_length', 42)
neckline_depth = params.get('neckline_depth', 8)
neckline_width = params.get('neckline_width', 7)
points = [
(0, 0),
(waist, 0),
(bust, bodice_length * 0.4),
(bust + 1, bodice_length * 0.7),
(shoulder, bodice_length),
(neckline_width, bodice_length - neckline_depth * 0.3),
(0, bodice_length - neckline_depth),
]
return {
'name': 'Front Bodice',
'points': points,
'grain_line': ((waist/2, 5), (waist/2, bodice_length - 5)),
'notches': [(bust, bodice_length * 0.4), (neckline_width, bodice_length - neckline_depth * 0.3)],
'labels': {'CF': (0, bodice_length/2), 'Bust Line': (bust/2, bodice_length * 0.4)},
'fold_line': [(0, 0), (0, bodice_length - neckline_depth)],
}
def generate_bodice_back(params: Dict) -> Dict:
"""Generate back bodice pattern piece."""
bust = params.get('bust', 90) / 4 + 1
waist = params.get('waist', 70) / 4 + 1
shoulder = params.get('shoulder_width', 40) / 2
bodice_length = params.get('bodice_length', 42)
back_neck_depth = params.get('back_neck_depth', 2)
neckline_width = params.get('neckline_width', 7)
points = [
(0, 0),
(waist, 0),
(bust, bodice_length * 0.4),
(bust + 1, bodice_length * 0.7),
(shoulder, bodice_length),
(neckline_width, bodice_length - back_neck_depth * 0.3),
(0, bodice_length - back_neck_depth),
]
return {
'name': 'Back Bodice',
'points': points,
'grain_line': ((waist/2, 5), (waist/2, bodice_length - 5)),
'notches': [(bust, bodice_length * 0.4)],
'labels': {'CB': (0, bodice_length/2)},
'fold_line': [(0, 0), (0, bodice_length - back_neck_depth)],
}
def generate_sleeve(params: Dict) -> Dict:
"""Generate sleeve pattern piece."""
sleeve_length = params.get('sleeve_length', 60)
bicep = params.get('bicep', 30) + 4
wrist = params.get('wrist', 18) + 2
cap_height = params.get('cap_height', 14)
half_bicep = bicep / 2
half_wrist = wrist / 2
points = [
(-half_wrist, 0),
(-half_bicep, sleeve_length - cap_height),
(-half_bicep * 0.7, sleeve_length - cap_height * 0.3),
(0, sleeve_length),
(half_bicep * 0.7, sleeve_length - cap_height * 0.3),
(half_bicep, sleeve_length - cap_height),
(half_wrist, 0),
]
return {
'name': 'Sleeve',
'points': points,
'grain_line': ((0, 5), (0, sleeve_length - 5)),
'notches': [(0, sleeve_length), (-half_bicep, sleeve_length - cap_height), (half_bicep, sleeve_length - cap_height)],
'labels': {'Grain': (2, sleeve_length/2)},
'quantity': 2,
}
def generate_skirt_front(params: Dict) -> Dict:
"""Generate front skirt panel."""
waist = params.get('waist', 70) / 4 + 1
hip = params.get('hip', 95) / 4 + 1
skirt_length = params.get('skirt_length', 55)
hip_depth = 20
flare = params.get('flare', 0)
hem_width = hip + flare
points = [
(0, 0),
(waist, 0),
(hip, hip_depth),
(hem_width, skirt_length),
(0, skirt_length),
]
return {
'name': 'Front Skirt',
'points': points,
'grain_line': ((waist/2, 5), (waist/2, skirt_length - 5)),
'notches': [(hip, hip_depth)],
'labels': {'CF': (0, skirt_length/2), 'Hip': (hip/2, hip_depth)},
'fold_line': [(0, 0), (0, skirt_length)],
}
def generate_skirt_back(params: Dict) -> Dict:
"""Generate back skirt panel."""
waist = params.get('waist', 70) / 4 + 2
hip = params.get('hip', 95) / 4 + 2
skirt_length = params.get('skirt_length', 55)
hip_depth = 20
flare = params.get('flare', 0)
hem_width = hip + flare
points = [
(0, 0),
(waist, 0),
(hip, hip_depth),
(hem_width, skirt_length),
(0, skirt_length),
]
return {
'name': 'Back Skirt',
'points': points,
'grain_line': ((waist/2, 5), (waist/2, skirt_length - 5)),
'notches': [(hip, hip_depth)],
'labels': {'CB': (0, skirt_length/2)},
'fold_line': [(0, 0), (0, skirt_length)],
}
def generate_collar(params: Dict) -> Dict:
"""Generate collar piece."""
collar_type = params.get('collar_type', 'standard')
collar_height = params.get('collar_height', 5)
neck_circumference = params.get('neck_circumference', 38) / 2 + 1
if collar_type == 'mandarin':
points = [
(0, 0), (neck_circumference, 0),
(neck_circumference, collar_height), (0, collar_height),
]
elif collar_type == 'peter_pan':
points = [
(0, 0), (neck_circumference, 0),
(neck_circumference + 3, collar_height * 0.5),
(neck_circumference + 5, collar_height),
(neck_circumference * 0.7, collar_height + 3),
(0, collar_height + 2),
]
else:
points = [
(0, 0), (neck_circumference, 0),
(neck_circumference + 2, collar_height * 0.7),
(neck_circumference - 1, collar_height), (0, collar_height),
]
return {
'name': f'Collar ({collar_type.title()})',
'points': points,
'grain_line': ((neck_circumference/2, 1), (neck_circumference/2, collar_height - 1)),
'notches': [(0, 0), (neck_circumference, 0)],
'labels': {'Neck Edge': (neck_circumference/2, -1.5)},
'quantity': 2,
}
def generate_pant_front(params: Dict) -> Dict:
"""Generate front pant leg piece."""
waist = params.get('waist', 70) / 4 + 1
hip = params.get('hip', 95) / 4 + 1
thigh = params.get('thigh', 55) / 2 + 2
knee = params.get('knee', 38) / 2 + 1
ankle = params.get('ankle', 24) / 2 + 1
pant_length = params.get('pant_length', 100)
crotch_depth = params.get('crotch_depth', 27)
half_thigh = thigh / 2
crotch_ext = hip * 0.25
points = [
(0, 0), (waist, 0),
(hip, crotch_depth * 0.6),
(half_thigh + 2, crotch_depth),
(knee/2 + 2, pant_length * 0.55),
(ankle/2 + 1, pant_length),
(-ankle/2 + 3, pant_length),
(-knee/2 + 5, pant_length * 0.55),
(-crotch_ext + 2, crotch_depth),
(0, crotch_depth * 0.3),
]
return {
'name': 'Front Pant',
'points': points,
'grain_line': ((waist/3, 5), (waist/3, pant_length - 5)),
'notches': [(hip, crotch_depth * 0.6), (0, 0)],
'labels': {'CF': (2, pant_length/2), 'Knee': (0, pant_length * 0.55)},
'quantity': 2,
}
def generate_pant_back(params: Dict) -> Dict:
"""Generate back pant leg piece."""
waist = params.get('waist', 70) / 4 + 2
hip = params.get('hip', 95) / 4 + 2
thigh = params.get('thigh', 55) / 2 + 3
knee = params.get('knee', 38) / 2 + 2
ankle = params.get('ankle', 24) / 2 + 2
pant_length = params.get('pant_length', 100)
crotch_depth = params.get('crotch_depth', 27)
half_thigh = thigh / 2
crotch_ext = hip * 0.35
points = [
(-2, 1), (waist + 1, 1),
(hip + 1, crotch_depth * 0.6),
(half_thigh + 3, crotch_depth),
(knee/2 + 3, pant_length * 0.55),
(ankle/2 + 2, pant_length),
(-ankle/2 + 2, pant_length),
(-knee/2 + 4, pant_length * 0.55),
(-crotch_ext, crotch_depth),
(-2, crotch_depth * 0.4),
]
return {
'name': 'Back Pant',
'points': points,
'grain_line': ((waist/3, 5), (waist/3, pant_length - 5)),
'notches': [(hip + 1, crotch_depth * 0.6)],
'labels': {'CB': (0, pant_length/2)},
'quantity': 2,
}
def generate_waistband(params: Dict) -> Dict:
"""Generate waistband piece."""
waist = params.get('waist', 70) / 2 + 3
band_height = params.get('waistband_height', 4)
points = [(0, 0), (waist, 0), (waist, band_height), (0, band_height)]
return {
'name': 'Waistband',
'points': points,
'grain_line': ((waist/2, 1), (waist/2, band_height - 1)),
'notches': [(0, 0), (waist/2, 0), (waist, 0)],
'labels': {'Grain': (waist/2, band_height + 1)},
'quantity': 1,
}
def generate_hood(params: Dict) -> Dict:
"""Generate hood pattern piece."""
head_circ = params.get('head_circumference', 57) / 2 + 3
hood_height = head_circ * 0.8
hood_depth = head_circ * 0.6
points = [
(0, 0), (0, hood_height * 0.3),
(-2, hood_height * 0.7), (3, hood_height),
(hood_depth * 0.5, hood_height + 3),
(hood_depth, hood_height - 2),
(hood_depth + 2, hood_height * 0.3),
(hood_depth - 3, 0),
]
return {
'name': 'Hood',
'points': points,
'grain_line': ((hood_depth/2, 5), (hood_depth/2, hood_height - 5)),
'notches': [(0, 0), (hood_depth - 3, 0)],
'labels': {'Face Opening': (-3, hood_height * 0.5)},
'quantity': 2,
}
def generate_pocket(params: Dict) -> Dict:
"""Generate pocket piece."""
pocket_width = params.get('pocket_width', 14)
pocket_height = params.get('pocket_height', 16)
pocket_type = params.get('pocket_type', 'patch')
if pocket_type == 'patch':
points = [
(0, 0), (pocket_width, 0),
(pocket_width, pocket_height),
(pocket_width * 0.8, pocket_height + 2),
(pocket_width * 0.2, pocket_height + 2),
(0, pocket_height),
]
else:
points = [
(0, 0), (pocket_width, 0),
(pocket_width, pocket_height * 0.3),
(0, pocket_height * 0.3),
]
return {
'name': f'Pocket ({pocket_type.title()})',
'points': points,
'grain_line': ((pocket_width/2, 1), (pocket_width/2, pocket_height - 1)),
'notches': [],
'labels': {},
'quantity': 2,
}
def generate_cuff(params: Dict) -> Dict:
"""Generate cuff piece."""
wrist = params.get('wrist', 18) + 3
cuff_height = params.get('cuff_height', 6)
points = [(0, 0), (wrist, 0), (wrist, cuff_height), (0, cuff_height)]
return {
'name': 'Cuff',
'points': points,
'grain_line': ((wrist/2, 1), (wrist/2, cuff_height - 1)),
'notches': [(0, 0), (wrist, 0)],
'labels': {},
'quantity': 2,
}
def get_pattern_pieces(garment_type: str, params: Dict) -> List[Dict]:
"""Get all pattern pieces for a garment type."""
pieces = []
garment_type = garment_type.lower()
if garment_type in ['shirt', 'blouse', 'top', 't-shirt', 'tee']:
pieces = [generate_bodice_front(params), generate_bodice_back(params), generate_sleeve(params)]
if params.get('has_collar', False):
pieces.append(generate_collar(params))
if params.get('has_cuffs', False):
pieces.append(generate_cuff(params))
if params.get('has_pockets', False):
pieces.append(generate_pocket(params))
elif garment_type in ['dress']:
pieces = [generate_bodice_front(params), generate_bodice_back(params), generate_sleeve(params),
generate_skirt_front(params), generate_skirt_back(params)]
if params.get('has_collar', False):
pieces.append(generate_collar(params))
elif garment_type in ['skirt']:
pieces = [generate_skirt_front(params), generate_skirt_back(params), generate_waistband(params)]
elif garment_type in ['pants', 'trousers', 'jeans']:
pieces = [generate_pant_front(params), generate_pant_back(params), generate_waistband(params)]
if params.get('has_pockets', False):
pieces.append(generate_pocket(params))
elif garment_type in ['jacket', 'coat', 'blazer']:
p = {**params, 'bodice_length': params.get('jacket_length', 65)}
pieces = [generate_bodice_front(p), generate_bodice_back(p), generate_sleeve(p)]
if params.get('has_collar', True):
pieces.append(generate_collar(p))
if params.get('has_pockets', True):
pieces.append(generate_pocket(p))
if params.get('has_hood', False):
pieces.append(generate_hood(p))
elif garment_type in ['hoodie', 'sweatshirt']:
p = {**params, 'bodice_length': params.get('jacket_length', 65)}
pieces = [generate_bodice_front(p), generate_bodice_back(p), generate_sleeve(p), generate_hood(p)]
if params.get('has_pockets', True):
p['pocket_type'] = 'patch'
pieces.append(generate_pocket(p))
elif garment_type in ['vest']:
p = {**params, 'bodice_length': params.get('vest_length', 55)}
pieces = [generate_bodice_front(p), generate_bodice_back(p)]
else:
pieces = [generate_bodice_front(params), generate_bodice_back(params), generate_sleeve(params)]
return pieces
def render_pattern_pieces(pieces: List[Dict], title: str = "2D Sewing Pattern") -> Image.Image:
"""Render pattern pieces as a clean technical drawing."""
n = len(pieces)
if n == 0:
fig, ax = plt.subplots(1, 1, figsize=(10, 8))
ax.text(0.5, 0.5, 'No pattern pieces generated', ha='center', va='center', fontsize=16)
ax.axis('off')
buf = BytesIO()
fig.savefig(buf, format='png', dpi=150, bbox_inches='tight', facecolor='white')
plt.close(fig)
buf.seek(0)
return Image.open(buf)
cols = min(3, n)
rows = math.ceil(n / cols)
fig, axes = plt.subplots(rows, cols, figsize=(7 * cols, 7 * rows))
fig.suptitle(title, fontsize=18, fontweight='bold', y=0.98)
if rows == 1 and cols == 1:
axes = np.array([axes])
axes = np.atleast_2d(axes)
for idx in range(rows * cols):
row, col = divmod(idx, cols)
ax = axes[row, col]
if idx >= n:
ax.axis('off')
continue
piece = pieces[idx]
points = piece['points']
color = PIECE_COLORS[idx % len(PIECE_COLORS)]
pts = points + [points[0]]
xs = [p[0] for p in pts]
ys = [p[1] for p in pts]
ax.fill(xs, ys, color=color, alpha=0.3, linewidth=0)
ax.plot(xs, ys, 'k-', linewidth=1.5, label='Cut line')
cx = np.mean([p[0] for p in points])
cy = np.mean([p[1] for p in points])
scale = 0.92
inner_xs = [cx + (x - cx) * scale for x in xs]
inner_ys = [cy + (y - cy) * scale for y in ys]
ax.plot(inner_xs, inner_ys, 'k--', linewidth=0.7, alpha=0.5, label='Seam line')
if 'fold_line' in piece:
fl = piece['fold_line']
ax.plot([fl[0][0], fl[1][0]], [fl[0][1], fl[1][1]], 'b-.', linewidth=1.2, label='Fold')
if 'grain_line' in piece:
gl = piece['grain_line']
ax.annotate('', xy=gl[1], xytext=gl[0],
arrowprops=dict(arrowstyle='->', color='red', lw=1.5))
mid_x = (gl[0][0] + gl[1][0]) / 2
mid_y = (gl[0][1] + gl[1][1]) / 2
ax.text(mid_x + 1, mid_y, 'GRAIN', fontsize=7, color='red', rotation=90,
ha='left', va='center', fontweight='bold')
if 'notches' in piece:
for nx, ny in piece['notches']:
ax.plot(nx, ny, 'k^', markersize=6)
qty = piece.get('quantity', 1)
qty_str = f" (×{qty})" if qty > 1 else " (×1 on fold)" if 'fold_line' in piece else ""
ax.set_title(f"{piece['name']}{qty_str}", fontsize=12, fontweight='bold', pad=10)
if 'labels' in piece:
for label, (lx, ly) in piece['labels'].items():
ax.text(lx, ly, label, fontsize=8, ha='center', va='center',
color='navy', style='italic',
bbox=dict(boxstyle='round,pad=0.2', facecolor='white', alpha=0.7, edgecolor='none'))
ax.set_aspect('equal')
ax.grid(True, alpha=0.2, linestyle=':')
ax.tick_params(labelsize=7)
ax.set_xlabel('cm', fontsize=8)
ax.set_ylabel('cm', fontsize=8)
if n > 0:
from matplotlib.lines import Line2D
legend_elements = [
Line2D([0], [0], color='k', linewidth=1.5, label='Cut line'),
Line2D([0], [0], color='k', linewidth=0.7, linestyle='--', label='Seam line'),
Line2D([0], [0], color='b', linewidth=1.2, linestyle='-.', label='Fold line'),
Line2D([0], [0], color='red', linewidth=1.5, marker='>', markersize=5, label='Grain line'),
Line2D([0], [0], color='k', linewidth=0, marker='^', markersize=6, label='Notch'),
]
fig.legend(handles=legend_elements, loc='lower center', ncol=5, fontsize=9,
bbox_to_anchor=(0.5, 0.01), frameon=True, fancybox=True)
plt.tight_layout(rect=[0, 0.04, 1, 0.96])
buf = BytesIO()
fig.savefig(buf, format='png', dpi=150, bbox_inches='tight', facecolor='white')
plt.close(fig)
buf.seek(0)
return Image.open(buf)
def generate_pattern_from_analysis(analysis: Dict) -> Tuple[Image.Image, str]:
"""Main entry: generate 2D pattern from garment analysis dict."""
garment_type = analysis.get('garment_type', 'shirt')
measurements = analysis.get('measurements', {})
features = analysis.get('features', {})
params = {**measurements, **features}
pieces = get_pattern_pieces(garment_type, params)
title = f"2D Sewing Pattern — {garment_type.title()}"
pattern_image = render_pattern_pieces(pieces, title)
summary_lines = [f"## Pattern: {garment_type.title()}\n"]
summary_lines.append(f"**Total pieces:** {len(pieces)}\n")
for i, piece in enumerate(pieces):
qty = piece.get('quantity', 1)
fold = " (cut on fold)" if 'fold_line' in piece else ""
summary_lines.append(f"{i+1}. **{piece['name']}** — Cut {qty}{fold}")
summary_lines.append(f"\n### Measurements Used (cm):")
for key, val in params.items():
if isinstance(val, (int, float)):
summary_lines.append(f"- {key.replace('_', ' ').title()}: {val}")
summary_lines.append(f"\n*Pattern includes 1cm seam allowance (dashed inner line).*")
summary_lines.append(f"*Arrows indicate grain line direction.*")
return pattern_image, "\n".join(summary_lines)
|