-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemeFormatting.py
68 lines (55 loc) · 2.54 KB
/
MemeFormatting.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
# https://github.com/danieldiekmeier/memegenerator
import PIL.Image as Image
import PIL.ImageFont as IFont
import PIL.ImageDraw as IDraw
import random as rand
import os
import posixpath
def top_bottom(memetype, topString, bottomString):
if memetype == "mocking-spongebob":
bottomString = ''
for i in range(len(topString)):
if rand.randint(0,1) == 0:
bottomString += topString[i].upper()
else:
bottomString += topString[i].lower()
if not os.path.exists("./New"):
os.mkdir("./New")
images = {}
for root, dirs, files in os.walk("./Templates"):
for filename in files:
name = filename.split('.')
images[name[0]] = root
location = images[memetype] + '/' + memetype + '.jpg'
with Image.open(location.strip()) as img:
size = img.size
fontSize = int(size[1] / 5)
font = IFont.truetype("impact.ttf", fontSize)
edit = IDraw.Draw(img)
# find biggest font size that works
topTextSize = font.getsize(topString)
bottomTextSize = font.getsize(bottomString)
while topTextSize[0] > size[0] - 20 or bottomTextSize[0] > size[0] - 20:
fontSize = fontSize - 1
font = IFont.truetype("impact.ttf", fontSize)
topTextSize = font.getsize(topString)
bottomTextSize = font.getsize(bottomString)
# find top centered position for top text
topTextPositionX = (size[0] / 2) - (topTextSize[0] / 2)
topTextPositionY = 0
topTextPosition = (topTextPositionX, topTextPositionY)
# find bottom centered position for bottom text
bottomTextPositionX = (size[0] / 2) - (bottomTextSize[0] / 2)
bottomTextPositionY = size[1] - bottomTextSize[1] - 10
bottomTextPosition = (bottomTextPositionX, bottomTextPositionY)
# 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):
edit.text((topTextPosition[0] + x, topTextPosition[1] + y), topString, (0, 0, 0), font=font)
edit.text((bottomTextPosition[0] + x, bottomTextPosition[1] + y), bottomString, (0, 0, 0), font=font)
edit.text(topTextPosition, topString, (255, 255, 255), font=font)
edit.text(bottomTextPosition, bottomString, (255, 255, 255), font=font)
img.save('New/' + memetype + '_new.jpg')
return 'New/' + memetype + '_new.jpg'