Commit
·
6231fde
1
Parent(s):
fa9cc42
Update app.py
Browse files
app.py
CHANGED
@@ -33,39 +33,27 @@ def generate_caption(image):
|
|
33 |
meme_text = response.choices[0].message.content
|
34 |
print(meme_text)
|
35 |
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
# Add the text to the image
|
45 |
-
draw = ImageDraw.Draw(meme_image)
|
46 |
-
|
47 |
-
# Calculate font size based on text length
|
48 |
-
text_length = len(meme_text)
|
49 |
-
if text_length <= 15:
|
50 |
-
font_size = 24
|
51 |
-
elif text_length <= 30:
|
52 |
-
font_size = 18
|
53 |
-
else:
|
54 |
-
font_size = 14
|
55 |
-
|
56 |
-
font = ImageFont.load_default()
|
57 |
text_width, text_height = draw.textsize(meme_text, font=font)
|
58 |
-
|
59 |
-
|
60 |
-
x = (meme_image.width - text_width) // 2
|
61 |
-
y = 10 # Adjust this value to add more or less padding from the top
|
62 |
|
63 |
-
draw.text((
|
64 |
|
65 |
-
# Convert the meme_image back to RGB mode
|
66 |
meme_image = meme_image.convert('RGB')
|
67 |
|
68 |
-
|
69 |
if torch.cuda.is_available():
|
70 |
torch.cuda.empty_cache()
|
71 |
|
|
|
33 |
meme_text = response.choices[0].message.content
|
34 |
print(meme_text)
|
35 |
|
36 |
+
image = Image.fromarray((image.permute(1, 2, 0).cpu().numpy() * 255).astype(np.uint8)).convert('RGB')
|
37 |
|
38 |
+
meme_image = image.copy()
|
39 |
+
draw = ImageDraw.Draw(meme_image)
|
40 |
|
41 |
+
font_size = 1
|
42 |
+
text_width = 0
|
43 |
+
max_width = int(224 * 0.9)
|
44 |
+
while text_width < max_width:
|
45 |
+
font_size += 1
|
46 |
+
font = ImageFont.truetype("impact.ttf", font_size)
|
47 |
+
text_width, _ = draw.textsize(meme_text, font=font)
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
text_width, text_height = draw.textsize(meme_text, font=font)
|
50 |
+
text_x = (224 - text_width) // 2
|
51 |
+
text_y = (224 - text_height) // 2
|
|
|
|
|
52 |
|
53 |
+
draw.text((text_x, 0), meme_text, font=font, fill=(255, 255, 255))
|
54 |
|
|
|
55 |
meme_image = meme_image.convert('RGB')
|
56 |
|
|
|
57 |
if torch.cuda.is_available():
|
58 |
torch.cuda.empty_cache()
|
59 |
|