andreinigo commited on
Commit
437e10c
1 Parent(s): d2a1b04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -26,7 +26,7 @@ def generate_caption(image):
26
  response = openai.ChatCompletion.create(
27
  model="gpt-4",
28
  messages=[
29
- {"role": "system", "content": "Escribe un meme chistoso para una imagen a partir en la descripci贸n dada por el usuario. No uses emojis. El meme tiene que ser corto y gracioso. El output del asistente solo debe ser el meme. Aseg煤rate que el meme sea tan bueno que se vuelva viral!"},
30
  {"role": "user", "content": caption}
31
  ],
32
  temperature=0.6
@@ -35,19 +35,19 @@ 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
- 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
 
51
  for line in text_lines:
52
  line_width, line_height = draw.textsize(line, font=font)
53
  x = (pil_image.width - line_width) // 2
 
26
  response = openai.ChatCompletion.create(
27
  model="gpt-4",
28
  messages=[
29
+ {"role": "system", "content": "Escribe un meme chistoso en espa帽ol para una imagen a partir en la descripci贸n dada por el usuario. No uses emojis. El meme tiene que ser corto y gracioso. El output del asistente solo debe ser el meme. Aseg煤rate que el meme sea tan bueno que se vuelva viral!"},
30
  {"role": "user", "content": caption}
31
  ],
32
  temperature=0.6
 
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
+ # Determine font size
42
+ max_width = int(pil_image.width * 0.9)
43
+ font_size = int(max_width / (len(meme_text) / 2))
44
  font = ImageFont.truetype("impact.ttf", font_size)
45
+
46
+ # Wrap the text to fit within the image width and have a maximum of 2 lines
47
+ wrapped_text = textwrap.fill(meme_text, width=int(max_width / font.getsize("A")[0]))
 
 
48
  text_lines = wrapped_text.split('\n')
49
+
50
+ y = 10
51
  for line in text_lines:
52
  line_width, line_height = draw.textsize(line, font=font)
53
  x = (pil_image.width - line_width) // 2