Commit
·
09527dd
1
Parent(s):
b97a813
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,24 +38,13 @@ def generate_caption(image):
|
|
| 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 =
|
| 46 |
font = ImageFont.truetype("impact.ttf", font_size)
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 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
|
|
|
|
| 38 |
# put the meme text on the image
|
| 39 |
draw = ImageDraw.Draw(pil_image)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
# Calculate font size
|
| 42 |
+
font_size = int(pil_image.width * 0.8) # Adjust the multiplier to change the font size based on image width
|
| 43 |
font = ImageFont.truetype("impact.ttf", font_size)
|
| 44 |
+
|
| 45 |
+
# Wrap the text to fit within the image width
|
| 46 |
+
wrapped_text = textwrap.fill(meme_text, width=int(pil_image.width / font_size * 0.8))
|
| 47 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# Calculate the position to place the text at the top and center horizontally
|
| 49 |
text_lines = wrapped_text.split('\n')
|
| 50 |
y = 10 # Adjust this value to add more or less padding from the top
|