-
Notifications
You must be signed in to change notification settings - Fork 38
/
memegenerator.py
110 lines (85 loc) · 2.76 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import sys
def make_meme(topString, bottomString, filename):
img = Image.open(filename)
imageSize = img.size
# find biggest font size that works
fontSize = int(imageSize[1]/5)
font = ImageFont.truetype("/Library/Fonts/Impact.ttf", fontSize)
topTextSize = font.getsize(topString)
bottomTextSize = font.getsize(bottomString)
while topTextSize[0] > imageSize[0]-20 or bottomTextSize[0] > imageSize[0]-20:
fontSize = fontSize - 1
font = ImageFont.truetype("/Library/Fonts/Impact.ttf", fontSize)
topTextSize = font.getsize(topString)
bottomTextSize = font.getsize(bottomString)
# 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 = int(fontSize/15)
for x in range(-outlineRange, outlineRange+1):
for y in range(-outlineRange, outlineRange+1):
draw.text((topTextPosition[0]+x, topTextPosition[1]+y), topString, (0,0,0), font=font)
draw.text((bottomTextPosition[0]+x, bottomTextPosition[1]+y), bottomString, (0,0,0), font=font)
draw.text(topTextPosition, topString, (255,255,255), font=font)
draw.text(bottomTextPosition, bottomString, (255,255,255), font=font)
img.save("temp.png")
def get_upper(somedata):
'''
Handle Python 2/3 differences in argv encoding
'''
result = ''
try:
result = somedata.decode("utf-8").upper()
except:
result = somedata.upper()
return result
def get_lower(somedata):
'''
Handle Python 2/3 differences in argv encoding
'''
result = ''
try:
result = somedata.decode("utf-8").lower()
except:
result = somedata.lower()
return result
if __name__ == '__main__':
args_len = len(sys.argv)
topString = ''
meme = 'standard'
if args_len == 1:
# no args except the launch of the script
print('args plz')
elif args_len == 2:
# only one argument, use standard meme
bottomString = get_upper(sys.argv[-1])
elif args_len == 3:
# args give meme and one line
bottomString = get_upper(sys.argv[-1])
meme = get_lower(sys.argv[1])
elif args_len == 4:
# args give meme and two lines
topString = get_upper(sys.argv[-2])
bottomString = get_upper(sys.argv[-1])
meme = get_lower(sys.argv[1])
else:
# so many args
# what do they mean
# too intense
print('to many argz')
print(meme)
filename = str(meme)+'.jpg'
make_meme(topString, bottomString, filename)