Amandeep01 commited on
Commit
56d6668
·
verified ·
1 Parent(s): db71e65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -13
app.py CHANGED
@@ -37,7 +37,9 @@ def get_default_font(size=20):
37
  font_paths = [
38
  "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
39
  "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
40
- "/usr/share/fonts/truetype/freefont/FreeSans.ttf"
 
 
41
  ]
42
 
43
  for path in font_paths:
@@ -132,23 +134,51 @@ def process_image(image, target_lang, overlay_opacity=0.7, progress=gr.Progress(
132
  translated = translate_text(text, target_lang)
133
  translations.append(f"{text} → {translated}")
134
 
135
- # Draw translation with background
136
- x, y = bbox[0][0], max(0, bbox[0][1] - 30)
137
 
138
- # Create semi-transparent background
139
- text_size = draw.textbbox((0, 0), translated, font=font)
140
- text_width = text_size[2] - text_size[0]
141
- text_height = text_size[3] - text_size[1]
 
 
 
 
 
142
 
143
- # Draw background rectangle
144
- bg_opacity = int(255 * overlay_opacity)
145
  draw.rectangle(
146
- [x - 5, y - 5, x + text_width + 5, y + text_height + 5],
147
- fill=(0, 0, 0, bg_opacity)
 
 
 
 
 
148
  )
149
 
150
- # Draw text
151
- draw.text((x, y), translated, fill=(255, 0, 0), font=font)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  # Join all translations
154
  all_translations = "\n".join(translations)
 
37
  font_paths = [
38
  "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
39
  "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
40
+ "/usr/share/fonts/truetype/freefont/FreeSans.ttf",
41
+ "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
42
+ "/usr/share/fonts/truetype/noto/NotoSans-Regular.ttf"
43
  ]
44
 
45
  for path in font_paths:
 
134
  translated = translate_text(text, target_lang)
135
  translations.append(f"{text} → {translated}")
136
 
137
+ # Extract bounding box coordinates
138
+ top_left, top_right, bottom_right, bottom_left = bbox
139
 
140
+ # Calculate text dimensions and position
141
+ # Use the original text bounding box size and position
142
+ x, y = top_left[0], top_left[1]
143
+ width = top_right[0] - top_left[0]
144
+ height = bottom_left[1] - top_left[1]
145
+
146
+ # Create a rectangle to cover the original text completely
147
+ # Add a bit of padding around the original text area
148
+ padding = 4
149
 
150
+ # Create solid background to cover original text
 
151
  draw.rectangle(
152
+ [
153
+ x - padding,
154
+ y - padding,
155
+ x + width + padding,
156
+ y + height + padding
157
+ ],
158
+ fill=(0, 0, 0, 255) # Solid black background to cover original text
159
  )
160
 
161
+ # Calculate font size to fit within the bounding box
162
+ # Start with a reasonable default size and adjust if needed
163
+ fontsize = min(20, int(width / (len(translated) * 0.5))) if translated else 20
164
+ fontsize = max(fontsize, 12) # Ensure minimum readability
165
+
166
+ # Get adjusted font
167
+ adjusted_font = get_default_font(size=fontsize)
168
+ if adjusted_font is None:
169
+ adjusted_font = font
170
+
171
+ # Get text size for centering
172
+ text_size = draw.textbbox((0, 0), translated, font=adjusted_font)
173
+ text_width = text_size[2] - text_size[0]
174
+ text_height = text_size[3] - text_size[1]
175
+
176
+ # Center text in the bounding box
177
+ text_x = x + (width - text_width) / 2
178
+ text_y = y + (height - text_height) / 2
179
+
180
+ # Draw text with contrasting color
181
+ draw.text((text_x, text_y), translated, fill=(255, 0, 0), font=adjusted_font)
182
 
183
  # Join all translations
184
  all_translations = "\n".join(translations)