Gopalag commited on
Commit
dfb11c1
·
verified ·
1 Parent(s): 8e31d11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -41
app.py CHANGED
@@ -18,14 +18,60 @@ pipe = DiffusionPipeline.from_pretrained(
18
  MAX_SEED = np.iinfo(np.int32).max
19
  MAX_IMAGE_SIZE = 2048
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def create_tshirt_preview(design_image, tshirt_color="white"):
22
  """
23
- Overlay the design onto the existing t-shirt template
24
  """
25
  # Load the template t-shirt image
26
  tshirt = Image.open('image.jpeg')
27
  tshirt_width, tshirt_height = tshirt.size
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # Convert design to PIL Image if it's not already
30
  if not isinstance(design_image, Image.Image):
31
  design_image = Image.fromarray(design_image)
@@ -37,7 +83,7 @@ def create_tshirt_preview(design_image, tshirt_color="white"):
37
 
38
  # Calculate position to center design on shirt
39
  x = (tshirt_width - design_width) // 2
40
- y = int(tshirt_height * 0.20) # Adjust this value based on your template
41
 
42
  # If design has transparency (RGBA), create mask
43
  if design_image.mode == 'RGBA':
@@ -50,41 +96,6 @@ def create_tshirt_preview(design_image, tshirt_color="white"):
50
 
51
  return tshirt
52
 
53
- # def create_tshirt_preview(design_image, tshirt_color="white"):
54
- # """
55
- # Overlay the design onto a t-shirt template
56
- # """
57
- # # Create a base t-shirt shape
58
- # tshirt_width = 800
59
- # tshirt_height = 1000
60
-
61
- # # Create base t-shirt image
62
- # tshirt = Image.new('RGB', (tshirt_width, tshirt_height), tshirt_color)
63
-
64
- # # Convert design to PIL Image if it's not already
65
- # if not isinstance(design_image, Image.Image):
66
- # design_image = Image.fromarray(design_image)
67
-
68
- # # Resize design to fit nicely on shirt (30% of shirt width)
69
- # design_width = int(tshirt_width * 0.3)
70
- # design_height = int(design_width * design_image.size[1] / design_image.size[0])
71
- # design_image = design_image.resize((design_width, design_height), Image.Resampling.LANCZOS)
72
-
73
- # # Calculate position to center design on shirt (top third of shirt)
74
- # x = (tshirt_width - design_width) // 2
75
- # y = int(tshirt_height * 0.25) # Position in top third
76
-
77
- # # If design has transparency (RGBA), create mask
78
- # if design_image.mode == 'RGBA':
79
- # mask = design_image.split()[3]
80
- # else:
81
- # mask = None
82
-
83
- # # Paste design onto shirt
84
- # tshirt.paste(design_image, (x, y), mask)
85
-
86
- # return tshirt
87
-
88
  def enhance_prompt_for_tshirt(prompt, style=None):
89
  """Add specific terms to ensure good t-shirt designs."""
90
  style_terms = {
@@ -97,9 +108,9 @@ def enhance_prompt_for_tshirt(prompt, style=None):
97
  }
98
 
99
  base_terms = [
100
- "create a t-shirt design",
101
  "with centered composition",
102
- "4k high quality",
103
  "professional design",
104
  "clear background"
105
  ]
@@ -201,14 +212,14 @@ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
201
  with gr.Column(elem_id="col-container"):
202
  gr.Markdown(
203
  """
204
- # 👕 Deradh's T-Shirt Design Generator
205
  """,
206
  elem_classes=["main-title"]
207
  )
208
 
209
  gr.Markdown(
210
  """
211
- Create unique t-shirt designs using AI.
212
  Describe your design idea and select a style to generate professional-quality artwork
213
  perfect for custom t-shirts.
214
  """,
 
18
  MAX_SEED = np.iinfo(np.int32).max
19
  MAX_IMAGE_SIZE = 2048
20
 
21
+ import numpy as np
22
+ from collections import Counter
23
+
24
+ def get_prominent_colors(image, num_colors=5):
25
+ """
26
+ Get the most prominent colors from an image, focusing on edges
27
+ """
28
+ # Convert to numpy array
29
+ img_array = np.array(image)
30
+
31
+ # Create a simple edge mask using gradient magnitude
32
+ gradient_x = np.gradient(img_array.mean(axis=2))[1]
33
+ gradient_y = np.gradient(img_array.mean(axis=2))[0]
34
+ gradient_magnitude = np.sqrt(gradient_x**2 + gradient_y**2)
35
+
36
+ # Threshold to get edge pixels
37
+ edge_threshold = np.percentile(gradient_magnitude, 90) # Adjust percentile as needed
38
+ edge_mask = gradient_magnitude > edge_threshold
39
+
40
+ # Get colors from edge pixels
41
+ edge_colors = img_array[edge_mask]
42
+
43
+ # Convert colors to tuples for counting
44
+ colors = [tuple(color) for color in edge_colors]
45
+
46
+ # Count occurrences of each color
47
+ color_counts = Counter(colors)
48
+
49
+ # Get most common colors
50
+ prominent_colors = color_counts.most_common(num_colors)
51
+
52
+ return prominent_colors
53
+
54
  def create_tshirt_preview(design_image, tshirt_color="white"):
55
  """
56
+ Overlay the design onto the existing t-shirt template and color match
57
  """
58
  # Load the template t-shirt image
59
  tshirt = Image.open('image.jpeg')
60
  tshirt_width, tshirt_height = tshirt.size
61
 
62
+ # Convert design to PIL Image if it's not already
63
+ if not isinstance(design_image, Image.Image):
64
+ design_image = Image.fromarray(design_image)
65
+
66
+ # Get prominent colors from the design
67
+ prominent_colors = get_prominent_colors(design_image)
68
+ if prominent_colors:
69
+ # Use the most prominent color for the t-shirt
70
+ main_color = prominent_colors[0][0] # RGB tuple of most common color
71
+ else:
72
+ # Fallback to white if no colors found
73
+ main_color = (255, 255, 255)
74
+
75
  # Convert design to PIL Image if it's not already
76
  if not isinstance(design_image, Image.Image):
77
  design_image = Image.fromarray(design_image)
 
83
 
84
  # Calculate position to center design on shirt
85
  x = (tshirt_width - design_width) // 2
86
+ y = int(tshirt_height * 0.2) # Adjust this value based on your template
87
 
88
  # If design has transparency (RGBA), create mask
89
  if design_image.mode == 'RGBA':
 
96
 
97
  return tshirt
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  def enhance_prompt_for_tshirt(prompt, style=None):
100
  """Add specific terms to ensure good t-shirt designs."""
101
  style_terms = {
 
108
  }
109
 
110
  base_terms = [
111
+ "create t-shirt design",
112
  "with centered composition",
113
+ "high quality",
114
  "professional design",
115
  "clear background"
116
  ]
 
212
  with gr.Column(elem_id="col-container"):
213
  gr.Markdown(
214
  """
215
+ # 👕Deradh's T-Shirt Design Generator
216
  """,
217
  elem_classes=["main-title"]
218
  )
219
 
220
  gr.Markdown(
221
  """
222
+ Create unique t-shirt designs using Deradh's AI.
223
  Describe your design idea and select a style to generate professional-quality artwork
224
  perfect for custom t-shirts.
225
  """,