1
|
|
|
#!/usr/bin/python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
from PIL import ImageFont |
4
|
|
|
from PIL import Image |
5
|
|
|
from PIL import ImageDraw |
6
|
|
|
import textwrap |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def make_meme(topString, bottomString, filename, extension, path, meme_font): |
10
|
|
|
img = Image.open(path + filename + extension) |
11
|
|
|
imageSize = img.size |
12
|
|
|
wrapwidth = int(imageSize[0]/20) |
13
|
|
|
|
14
|
|
|
# wrap input text strings |
15
|
|
|
if bottomString is None: |
16
|
|
|
bottomString = [" "] |
17
|
|
|
else: |
18
|
|
|
bottomString = textwrap.wrap(bottomString ,width=wrapwidth) |
19
|
|
|
if topString is None: |
20
|
|
|
topString = [" "] |
21
|
|
|
else: |
22
|
|
|
topString = textwrap.wrap(topString ,width=wrapwidth) |
23
|
|
|
|
24
|
|
|
# longest line to find font size |
25
|
|
|
longestTopString = max(topString, key=len) |
26
|
|
|
longestBottomString = max(bottomString, key=len) |
27
|
|
|
|
28
|
|
|
# find biggest font size that works |
29
|
|
|
fontSize = int(imageSize[1]/6) |
30
|
|
|
font = ImageFont.truetype(meme_font, fontSize) |
31
|
|
|
topTextSize = font.getsize(longestTopString) |
32
|
|
|
bottomTextSize = font.getsize(longestBottomString) |
33
|
|
|
while topTextSize[0] > imageSize[0]-20 or bottomTextSize[0] > imageSize[0]-20: |
34
|
|
|
fontSize = fontSize - 1 |
35
|
|
|
font = ImageFont.truetype(meme_font, fontSize) |
36
|
|
|
topTextSize = font.getsize(longestTopString) |
37
|
|
|
bottomTextSize = font.getsize(longestBottomString) |
38
|
|
|
|
39
|
|
|
# find top centered position for top text |
40
|
|
|
topPositions = [] |
41
|
|
|
initialX = 0 - topTextSize[1] |
42
|
|
|
for i in topString: |
43
|
|
|
topTextLineSize = font.getsize(i) |
44
|
|
|
topTextPositionX = (imageSize[0]/2) - (topTextLineSize[0]/2) |
45
|
|
|
topTextPositionY = initialX + topTextSize[1] |
46
|
|
|
initialX = topTextPositionY |
47
|
|
|
topTextPosition = (topTextPositionX, topTextPositionY) |
48
|
|
|
topPositions.append(topTextPosition) |
49
|
|
|
|
50
|
|
|
# find bottom centered position for bottom text |
51
|
|
|
bottomPositions = [] |
52
|
|
|
initialY = imageSize[1] - bottomTextSize[1]*len(bottomString)-15 - bottomTextSize[1] |
53
|
|
|
for i in bottomString: |
54
|
|
|
bottomTextLineSize = font.getsize(i) |
55
|
|
|
bottomTextPositionX = (imageSize[0]/2) - (bottomTextLineSize[0]/2) |
56
|
|
|
bottomTextPositionY = initialY + bottomTextSize[1] |
57
|
|
|
initialY = bottomTextPositionY |
58
|
|
|
bottomTextPosition = (bottomTextPositionX, bottomTextPositionY) |
59
|
|
|
bottomPositions.append(bottomTextPosition) |
60
|
|
|
|
61
|
|
|
draw = ImageDraw.Draw(img) |
62
|
|
|
|
63
|
|
|
# draw outlines |
64
|
|
|
outlineRange = int(fontSize/25)+1 |
65
|
|
|
for x in range(-outlineRange, outlineRange+1): |
66
|
|
|
for y in range(-outlineRange, outlineRange+1): |
67
|
|
|
ct = 0 |
68
|
|
|
cb = 0 |
69
|
|
|
for i in topPositions: |
70
|
|
|
draw.text((i[0]+x, i[1]+y), topString[ct], (0, 0, 0), font=font) |
71
|
|
|
ct += 1 |
72
|
|
|
for i in bottomPositions: |
73
|
|
|
draw.text((i[0]+x, i[1]+y), bottomString[cb], (0, 0, 0), font=font) |
74
|
|
|
cb += 1 |
75
|
|
|
|
76
|
|
|
for i in range(len(topString)): |
77
|
|
|
draw.text(topPositions[i], topString[i], (255, 255, 255), font=font) |
78
|
|
|
for i in range(len(bottomString)): |
79
|
|
|
draw.text(bottomPositions[i], bottomString[i], (255, 255, 255), font=font) |
80
|
|
|
|
81
|
|
|
img.save(path+filename+"-meme"+extension) |
82
|
|
|
|