-
Notifications
You must be signed in to change notification settings - Fork 3
/
memegenerator.py
53 lines (43 loc) · 1.85 KB
/
memegenerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import os
font_path = "static/fonts/Impact.ttf"
def gen_meme(image_name, top, bottom, meme_id):
top = top.upper()
bottom = bottom.upper()
image_path = os.path.join('static/images',image_name)
img = Image.open(str(image_path))
imageSize = img.size
# find biggest font size that works
fontSize = imageSize[1]/5
font = ImageFont.truetype(font_path, fontSize)
topTextSize = font.getsize(top)
bottomTextSize = font.getsize(bottom)
while topTextSize[0] > imageSize[0]-20 or bottomTextSize[0] > imageSize[0]-20:
fontSize = fontSize - 1
font = ImageFont.truetype(font_path, fontSize)
topTextSize = font.getsize(top)
bottomTextSize = font.getsize(bottom)
# find top centered position for top text
topTextPositionX = (imageSize[0]/2) - (topTextSize[0]/2)
topTextPositionY = 0
topTextPosition = (topTextPositionX, topTextPositionY)
# find bottom centered position for bottom text
bottomTextPositionX = (imageSize[0]/2) - (bottomTextSize[0]/2)
bottomTextPositionY = imageSize[1] - bottomTextSize[1]
bottomTextPosition = (bottomTextPositionX, bottomTextPositionY)
draw = ImageDraw.Draw(img)
# draw outlines
# there may be a better way
outlineRange = fontSize/15
for x in range(-outlineRange, outlineRange+1):
for y in range(-outlineRange, outlineRange+1):
draw.text((topTextPosition[0]+x, topTextPosition[1]+y), top, (0,0,0), font=font)
draw.text((bottomTextPosition[0]+x, bottomTextPosition[1]+y), bottom, (0,0,0), font=font)
draw.text(topTextPosition, top, (255,255,255), font=font)
draw.text(bottomTextPosition, bottom, (255,255,255), font=font)
img.save("static/memes/%s.png" % meme_id)
if __name__ == '__main__':
gen_meme("Hello", "World", "aliens")