andreinigo commited on
Commit
b97a813
·
1 Parent(s): 3bf9160

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -35,18 +35,27 @@ def generate_caption(image):
35
  meme_text = response.choices[0].message.content
36
  print(meme_text)
37
 
38
- #put the meme text on the image
39
  draw = ImageDraw.Draw(pil_image)
40
 
 
 
 
41
  # Calculate font size
42
- text_length = len(meme_text)
43
- font_size = 24
44
  font = ImageFont.truetype("impact.ttf", font_size)
45
- text_width, text_height = draw.textsize(meme_text, font=font)
46
-
47
- # Wrap the text to fit within the image width
48
- wrapped_text = textwrap.fill(meme_text, width=int(pil_image.width / font_size))
49
-
 
 
 
 
 
 
 
50
  # Calculate the position to place the text at the top and center horizontally
51
  text_lines = wrapped_text.split('\n')
52
  y = 10 # Adjust this value to add more or less padding from the top
 
35
  meme_text = response.choices[0].message.content
36
  print(meme_text)
37
 
38
+ # put the meme text on the image
39
  draw = ImageDraw.Draw(pil_image)
40
 
41
+ # Set the maximum number of lines to 2
42
+ max_lines = 2
43
+
44
  # Calculate font size
45
+ font_size = 1
 
46
  font = ImageFont.truetype("impact.ttf", font_size)
47
+
48
+ while True:
49
+ text_width, text_height = draw.textsize(meme_text, font=font)
50
+ wrapped_text = textwrap.fill(meme_text, width=int(pil_image.width / font_size))
51
+ num_lines = len(wrapped_text.split('\n'))
52
+
53
+ if num_lines <= max_lines:
54
+ break
55
+
56
+ font_size += 1
57
+ font = ImageFont.truetype("impact.ttf", font_size)
58
+
59
  # Calculate the position to place the text at the top and center horizontally
60
  text_lines = wrapped_text.split('\n')
61
  y = 10 # Adjust this value to add more or less padding from the top