Víctor Sáez commited on
Commit
9b802a0
·
1 Parent(s): 2e9147d

Ajust text size dinamically

Browse files
Files changed (1) hide show
  1. app.py +11 -2
app.py CHANGED
@@ -44,7 +44,7 @@ if not font_path.exists():
44
  print(f"Font file {font_path} not found. Using default font.")
45
  font = ImageFont.load_default()
46
  else:
47
- font = ImageFont.truetype(str(font_path), size=100) # Reduced font size
48
 
49
  # Set up translations for the app
50
  translations = {
@@ -247,12 +247,21 @@ def detect_objects(image, language_selector, translated_model_selector, threshol
247
 
248
  # Calculate text position and size
249
  try:
 
 
 
 
 
 
250
  text_bbox = draw.textbbox((0, 0), display_text, font=font)
251
  text_width = text_bbox[2] - text_bbox[0]
252
  text_height = text_bbox[3] - text_bbox[1]
253
  except:
254
  # Fallback for older PIL versions
255
- text_width, text_height = draw.textsize(display_text, font=font)
 
 
 
256
 
257
  # Draw text background
258
  text_bg = [
 
44
  print(f"Font file {font_path} not found. Using default font.")
45
  font = ImageFont.load_default()
46
  else:
47
+ font = ImageFont.truetype(str(font_path), size=8) # Reduced font size
48
 
49
  # Set up translations for the app
50
  translations = {
 
247
 
248
  # Calculate text position and size
249
  try:
250
+
251
+ image_width = image.size[0]
252
+ # Calculate the font size for drawing labels, ensuring it scales with image width but is never smaller than 50 pixels.
253
+ font_size = max(image_width // 40, 12) # Adjust font size based on image width
254
+ font = ImageFont.truetype(str(font_path), size=font_size)
255
+
256
  text_bbox = draw.textbbox((0, 0), display_text, font=font)
257
  text_width = text_bbox[2] - text_bbox[0]
258
  text_height = text_bbox[3] - text_bbox[1]
259
  except:
260
  # Fallback for older PIL versions
261
+ text_bbox = draw.textbbox((0, 0), display_text, font=font)
262
+ text_width = text_bbox[2] - text_bbox[0]
263
+ text_height = text_bbox[3] - text_bbox[1]
264
+
265
 
266
  # Draw text background
267
  text_bg = [