umarabbas890 commited on
Commit
c62203f
·
verified ·
1 Parent(s): 385fe2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -2
app.py CHANGED
@@ -31,8 +31,9 @@ def create_text_image(text):
31
  except IOError:
32
  font = ImageFont.load_default()
33
 
34
- # Calculate text position to center it
35
- text_width, text_height = font.getsize(text)
 
36
  text_x = (width - text_width) // 2
37
  text_y = (height - text_height) // 2
38
 
@@ -45,6 +46,7 @@ def create_text_image(text):
45
  img_byte_arr.seek(0)
46
  return img_byte_arr
47
 
 
48
  # Run the app
49
  if __name__ == "__main__":
50
  main()
 
31
  except IOError:
32
  font = ImageFont.load_default()
33
 
34
+ # Calculate text position to center it using textbbox
35
+ text_bbox = draw.textbbox((0, 0), text, font=font)
36
+ text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1]
37
  text_x = (width - text_width) // 2
38
  text_y = (height - text_height) // 2
39
 
 
46
  img_byte_arr.seek(0)
47
  return img_byte_arr
48
 
49
+
50
  # Run the app
51
  if __name__ == "__main__":
52
  main()