mostlycached commited on
Commit
2023379
·
verified ·
1 Parent(s): 0f221db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -48
app.py CHANGED
@@ -56,8 +56,8 @@ def text_behind_image(input_image, text, text_color, font_size, text_opacity):
56
  if not text:
57
  text = "SAMPLE TEXT"
58
 
59
- # Set up the font
60
- font_size = max(10, int(min(width, height) * (font_size / 100)))
61
 
62
  # Try several fonts
63
  font = None
@@ -80,55 +80,38 @@ def text_behind_image(input_image, text, text_color, font_size, text_opacity):
80
 
81
  if font is None:
82
  font = ImageFont.load_default()
83
-
84
- # For short text, create a repeating pattern across the entire image
85
- if len(text) < 20:
86
- repeated_text = (text + " ") * 20
87
- line_height = int(font_size * 1.5)
88
-
89
- # Draw text in a grid pattern covering the entire image
90
- for y in range(0, height, line_height):
91
- # Offset each line for a more interesting pattern
92
- offset = (y // line_height) % 20
93
- line = repeated_text[offset:] + repeated_text[:offset]
94
-
95
- # Draw the text
96
- draw.text((10, y), line, font=font, fill=text_color_rgb + (int(text_opacity * 255),))
97
-
98
- else:
99
- # For longer text, do proper word wrapping
100
- margin = 20
101
- max_width = width - 2 * margin
102
-
103
- # Calculate approx chars per line (estimating 0.6 × font_size per char width)
104
- chars_per_line = max(10, int(max_width / (font_size * 0.6)))
105
-
106
- # Word wrap
107
- wrapper = textwrap.TextWrapper(width=chars_per_line)
108
- lines = wrapper.wrap(text)
109
-
110
- # Calculate total text height
111
- line_height = int(font_size * 1.5)
112
- text_height = len(lines) * line_height
113
-
114
- # Calculate starting Y position (centered)
115
- start_y = (height - text_height) // 2
116
-
117
- # Draw each line
118
- for i, line in enumerate(lines):
119
- # Estimate line width (better than using font.getbbox which can fail)
120
- line_width = min(max_width, len(line) * int(font_size * 0.6))
121
-
122
- # Center the line
123
- x = (width - line_width) // 2
124
- y = start_y + (i * line_height)
125
-
126
- # Draw the text with opacity
127
- draw.text((x, y), line, font=font, fill=text_color_rgb + (int(text_opacity * 255),))
128
 
129
  # Step 5: Composite all layers together
130
  # First, composite the text on top of the background
131
- # We use the inverted mask so text only shows where the person isn't
132
  background_with_text = Image.alpha_composite(background, text_layer)
133
 
134
  # Then, composite the person on top
 
56
  if not text:
57
  text = "SAMPLE TEXT"
58
 
59
+ # Set up the font size - make it larger since we're just placing it once
60
+ font_size = max(20, int(min(width, height) * (font_size / 100)))
61
 
62
  # Try several fonts
63
  font = None
 
80
 
81
  if font is None:
82
  font = ImageFont.load_default()
83
+
84
+ # Get text dimensions
85
+ # Try to use getbbox first for newer PIL versions
86
+ try:
87
+ text_width = font.getbbox(text)[2] - font.getbbox(text)[0]
88
+ text_height = font.getbbox(text)[3] - font.getbbox(text)[1]
89
+ except:
90
+ # Fallback for older PIL versions
91
+ try:
92
+ text_width = font.getsize(text)[0]
93
+ text_height = font.getsize(text)[1]
94
+ except:
95
+ # Second fallback - estimate size
96
+ text_width = len(text) * (font_size * 0.6)
97
+ text_height = font_size * 1.2
98
+
99
+ # Position in the upper middle section
100
+ # Divide the image into a 3x3 grid and put the text in the top-center cell
101
+ grid_width = width // 3
102
+ grid_height = height // 3
103
+
104
+ # Center horizontally in the middle third
105
+ x_center = width // 2 - text_width // 2
106
+
107
+ # Position vertically in the top third
108
+ y_top = grid_height // 2 - text_height // 2
109
+
110
+ # Draw the text with specified opacity and color
111
+ draw.text((x_center, y_top), text, font=font, fill=text_color_rgb + (int(text_opacity * 255),))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  # Step 5: Composite all layers together
114
  # First, composite the text on top of the background
 
115
  background_with_text = Image.alpha_composite(background, text_layer)
116
 
117
  # Then, composite the person on top