|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
from PIL import ImageFont |
|
3
|
|
|
from PIL import Image |
|
4
|
|
|
from PIL import ImageDraw |
|
5
|
|
|
|
|
6
|
|
|
import sys |
|
|
|
|
|
|
7
|
|
|
import yaml |
|
8
|
|
|
|
|
9
|
|
|
# import paths |
|
10
|
|
|
with open("config.yml", "r") as f: |
|
11
|
|
|
memes_folder = yaml.load(f)["path"]["memes"] |
|
12
|
|
|
with open("config.yml", "r") as f: |
|
13
|
|
|
meme_font = yaml.load(f)["path"]["meme_font"] |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
def make_meme(topString, bottomString, filename, extension): |
|
17
|
|
|
ext = extension |
|
18
|
|
|
img = Image.open(filename) |
|
19
|
|
|
imageSize = img.size |
|
20
|
|
|
|
|
21
|
|
|
# find biggest font size that works |
|
22
|
|
|
fontSize = int(imageSize[1]/6) |
|
23
|
|
|
font = ImageFont.truetype(meme_font, fontSize) |
|
24
|
|
|
topTextSize = font.getsize(topString) |
|
25
|
|
|
bottomTextSize = font.getsize(bottomString) |
|
26
|
|
|
while topTextSize[0] > imageSize[0]-20 or bottomTextSize[0] > imageSize[0]-20: |
|
27
|
|
|
fontSize = fontSize - 1 |
|
28
|
|
|
font = ImageFont.truetype(meme_font, fontSize) |
|
29
|
|
|
topTextSize = font.getsize(topString) |
|
30
|
|
|
bottomTextSize = font.getsize(bottomString) |
|
31
|
|
|
|
|
32
|
|
|
# find top centered position for top text |
|
33
|
|
|
topTextPositionX = (imageSize[0]/2) - (topTextSize[0]/2) |
|
34
|
|
|
topTextPositionY = 0 |
|
35
|
|
|
topTextPosition = (topTextPositionX, topTextPositionY) |
|
36
|
|
|
|
|
37
|
|
|
# find bottom centered position for bottom text |
|
38
|
|
|
bottomTextPositionX = (imageSize[0]/2) - (bottomTextSize[0]/2) |
|
39
|
|
|
bottomTextPositionY = imageSize[1] - bottomTextSize[1]-15 |
|
40
|
|
|
bottomTextPosition = (bottomTextPositionX, bottomTextPositionY) |
|
41
|
|
|
|
|
42
|
|
|
draw = ImageDraw.Draw(img) |
|
43
|
|
|
|
|
44
|
|
|
# draw outlines |
|
45
|
|
|
# there may be a better way |
|
46
|
|
|
outlineRange = int(fontSize/15) |
|
47
|
|
|
for x in range(-outlineRange, outlineRange+1): |
|
48
|
|
|
for y in range(-outlineRange, outlineRange+1): |
|
49
|
|
|
draw.text((topTextPosition[0]+x, topTextPosition[1]+y), topString, (0, 0, 0), font=font) |
|
50
|
|
|
draw.text((bottomTextPosition[0]+x, bottomTextPosition[1]+y), bottomString, (0, 0, 0), font=font) |
|
51
|
|
|
|
|
52
|
|
|
draw.text(topTextPosition, topString, (255, 255, 255), font=font) |
|
53
|
|
|
draw.text(bottomTextPosition, bottomString, (255, 255, 255), font=font) |
|
54
|
|
|
|
|
55
|
|
|
img.save(memes_folder+"meme"+ext) |
|
56
|
|
|
|