gaur3009 commited on
Commit
44ba25a
·
verified ·
1 Parent(s): 215c3c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -32
app.py CHANGED
@@ -16,43 +16,19 @@ def generate_text_image(text, style, width, height):
16
  draw = ImageDraw.Draw(img)
17
 
18
  # Dynamically calculate font size based on image dimensions
19
- font_size = int(min(width, height) * 0.2) # Adjust text to be a larger proportion of the image
20
  try:
21
  font = ImageFont.truetype(font_path[style], size=font_size)
22
  except:
23
  font = ImageFont.load_default()
24
 
25
- # Word wrapping to fit text within the image
26
- max_width = width - 20 # Leave some padding
27
- words = text.split()
28
- lines = []
29
- current_line = ""
30
-
31
- for word in words:
32
- test_line = f"{current_line} {word}".strip()
33
- test_width, _ = draw.textsize(test_line, font=font)
34
- if test_width <= max_width:
35
- current_line = test_line
36
- else:
37
- lines.append(current_line)
38
- current_line = word
39
-
40
- if current_line:
41
- lines.append(current_line)
42
-
43
- # Calculate total text block height
44
- line_height = draw.textsize("Test", font=font)[1]
45
- total_text_height = line_height * len(lines)
46
-
47
- # Start position for vertically centered text
48
- y_position = (height - total_text_height) // 2
49
-
50
- # Draw each line of text
51
- for line in lines:
52
- text_width, _ = draw.textsize(line, font=font)
53
- x_position = (width - text_width) // 2
54
- draw.text((x_position, y_position), line, font=font, fill=(0, 0, 0, 255))
55
- y_position += line_height
56
 
57
  return img
58
 
@@ -76,6 +52,26 @@ def apply_displacement_map(text_img, clothing_img, strength=20):
76
 
77
  return text_warped
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  def overlay_text_on_clothing(clothing_image, text_input, style, strength=20, alpha=0.7, photo=None, blend_alpha=0.5):
80
  """Blend generated text and optional photo onto the clothing image."""
81
  clothing_img = cv2.imread(clothing_image) # Read image from file path
 
16
  draw = ImageDraw.Draw(img)
17
 
18
  # Dynamically calculate font size based on image dimensions
19
+ font_size = int(min(width, height) * 0.1) # Adjust text to be proportional
20
  try:
21
  font = ImageFont.truetype(font_path[style], size=font_size)
22
  except:
23
  font = ImageFont.load_default()
24
 
25
+ text_bbox = draw.textbbox((0, 0), text, font=font)
26
+ text_width = text_bbox[2] - text_bbox[0]
27
+ text_height = text_bbox[3] - text_bbox[1]
28
+
29
+ position = ((width - text_width) // 2, (height - text_height) // 2)
30
+
31
+ draw.text(position, text, font=font, fill=(0, 0, 0, 255))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  return img
34
 
 
52
 
53
  return text_warped
54
 
55
+ def apply_displacement_map(text_img, clothing_img, strength=20):
56
+ """Apply displacement map to blend text onto clothing."""
57
+ gray = cv2.cvtColor(clothing_img, cv2.COLOR_BGR2GRAY)
58
+ grad_x = cv2.Sobel(gray, cv2.CV_32F, 1, 0, ksize=5)
59
+ grad_y = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=5)
60
+
61
+ grad_x = cv2.normalize(grad_x, None, 0, 1, cv2.NORM_MINMAX)
62
+ grad_y = cv2.normalize(grad_y, None, 0, 1, cv2.NORM_MINMAX)
63
+
64
+ displacement_map = np.zeros_like(clothing_img, dtype=np.float32)
65
+ displacement_map[:, :, 0] = grad_x * strength
66
+ displacement_map[:, :, 1] = grad_y * strength
67
+
68
+ text_warped = cv2.remap(text_img,
69
+ displacement_map[:, :, 0].astype(np.float32),
70
+ displacement_map[:, :, 1].astype(np.float32),
71
+ interpolation=cv2.INTER_LINEAR)
72
+
73
+ return text_warped
74
+
75
  def overlay_text_on_clothing(clothing_image, text_input, style, strength=20, alpha=0.7, photo=None, blend_alpha=0.5):
76
  """Blend generated text and optional photo onto the clothing image."""
77
  clothing_img = cv2.imread(clothing_image) # Read image from file path