-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeme.py
executable file
·68 lines (60 loc) · 2.16 KB
/
meme.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
"""Meme cli."""
import os
import random
import argparse
from QuoteEngine import Ingestor, QuoteModel
from MemeEngine import MemeEngine
def generate_meme(path=None, body=None, author=None):
"""Generate a meme given a path and a quote."""
img = None
quote = None
if path is None:
images = "./_data/photos/dog/"
imgs = []
# add all images from image folder to imgs
for root, dirs, files in os.walk(images):
imgs = [os.path.join(root, name) for name in files]
# Select image at random
img = random.choice(imgs)
else:
img = path
if body is None:
quote_files = [
'./_data/DogQuotes/DogQuotesTXT.txt',
'./_data/DogQuotes/DogQuotesDOCX.docx',
'./_data/DogQuotes/DogQuotesCSV.csv',
'./_data/DogQuotes/DogQuotesPDF.pdf'
]
quotes = []
for file in quote_files:
quotes.extend(Ingestor.parse(file))
quote = random.choice(quotes)
else:
if author is None:
raise Exception('Author Required if Body is Used')
quote = QuoteModel(body, author)
meme = MemeEngine('./static')
path = meme.make_meme(img, quote.body, quote.author)
return path
if __name__ == "__main__":
# Create the parser
parser = argparse.ArgumentParser(description='Meme generator CLI')
# Add the arguments
parser.add_argument('--path',
metavar='image path',
type=str,
default=None,
help='the path for an image file')
parser.add_argument('--body',
metavar='quote body',
type=str,
default=None,
help='the body of the quote')
parser.add_argument('--author',
metavar='quote author',
type=str,
default=None,
help='the author of the quote')
# Execute parse_args()
args = parser.parse_args()
print(generate_meme(args.path, args.body, args.author))