forked from Demenkop/motya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motya.py
65 lines (52 loc) · 2.17 KB
/
motya.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
import os
import random
import time
import typer
from typer import Option, Typer, Argument
from pathlib import Path
from PIL import Image
from motya.font import Font
from motya.motya import Motya
from motya.template import Template, Frame
app = Typer()
@app.command(help="Motya - генератор демотиваторов на python")
def create(
phrase: Path = Argument(None),
image: Path = Argument(None),
template: Path = Option("template.jpg", help="Путь к штаблону демотиватора"), # noqa
images: Path = Option("images/", help="Путь к картинкам для мемов"),
phrases: Path = Option("phrases.txt", help="Путь к файлу с фразами"),
font: Path = Option("times.ttf", help="Путь к шрифту"),
font_size: int = Option(45, help="Размер шрифта, по умолчанию 45"),
seed: str = Option(time.time(), help="Seed для создания картинки"),
output: Path = Option("result.png", help="Куда сохранять мем")
):
print(f"Seed: {seed}")
random.seed(seed)
main_template = Template(
image=Image.open(template),
width=574,
height=522,
frame=Frame(x_start=75, y_start=45, x_end=499, y_end=373),
padding=10,
)
file_list = []
# noinspection PyTypeChecker
for dirpath, dirnames, filenames in os.walk(images):
for filename in [f for f in filenames if f.endswith(("png", "jpg", "jpeg"))]: # noqa: E501
# noinspection PyTypeChecker
file_list.append(Image.open(os.path.join(images, filename)))
# noinspection PyTypeChecker
font = Font(str(font), size=font_size, font_y=390)
# noinspection PyTypeChecker
motya = Motya(
template=main_template,
phrases=list(open(phrases, encoding="utf-8").readlines()) if phrase is None else [phrase], # noqa: E501
images=list(file_list) if image is None else [Image.open(image)],
font=font
)
output_img = motya.generate()
output_img.save(output)
output_img.show()
if __name__ == '__main__':
app()