forked from KeyZenD/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperDemotivator.py
More file actions
174 lines (151 loc) · 5.2 KB
/
SuperDemotivator.py
File metadata and controls
174 lines (151 loc) · 5.2 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from .. import loader, utils
import logging
from PIL import Image, ImageDraw, ImageOps, ImageFont
from textwrap import wrap
import io
import requests
# https://t.me/KeyZenD
# https://t.me/SomeScripts
logger = logging.getLogger(__name__)
@loader.tds
class DeMoTiVaToRsMod(loader.Module):
"""Демотиваторы на картинки от @SomeScripts by @DneZyeK"""
strings = {
"name": "SuperDemotivator"
}
async def client_ready(self, client, db):
self.client = client
@loader.owner
async def demoticmd(self, message):
"""текст + фото или ответ на фото
не мнёт фотки"""
await cmds(message, 0)
async def demotcmd(self, message):
"""текст + фото или ответ на фото
мнёт фотки"""
await cmds(message, 1)
async def cmds(message, type):
event, is_reply = await check_media(message)
if not event:
await message.edit("<b>Ответ командой на картинку!</b>")
return
text = utils.get_args_raw(message)
if not text:
await message.edit("<b>Команду нужно использовать с текстом!</b>")
return
await message.edit("<b>Демотивирую...</b>")
bytes_image = await event.download_media(bytes)
demotivator = await demotion(font_bytes, bytes_image, text, type)
if is_reply:
a = await event.reply(file=demotivator)
await message.delete()
return a
else:
return await event.edit(file=demotivator, text="")
async def check_media(message):
reply = await message.get_reply_message()
is_reply = True
if not reply:
reply = message
is_reply = False
if not reply.file:
return False, ...
mime = reply.file.mime_type.split("/")[0].lower()
if mime != "image":
return False, ...
return reply, is_reply
async def textwrap(text, length=50, splitter = "\n\n"):
out = []
lines = text.rsplit(splitter, 1)
for text in lines:
txt = []
parts = text.split("\n")
for part in parts:
part = "\n".join(wrap(part, length))
txt.append(part)
text = "\n".join(txt)
out.append(text)
return out
async def draw_main(
bytes_image,
type,
frame_width_1 = 5,
frame_fill_1 = (0, 0, 0),
frame_width_2 = 3,
frame_fill_2 = (255, 255, 255),
expand_proc = 10,
main_fill = (0, 0, 0)
):
main_ = Image.open(io.BytesIO(bytes_image))
main = Image.new("RGB", main_.size, "black")
main.paste(main_, (0, 0))
if type == 1:
main = main.resize((700, 550))
main = ImageOps.expand(main, frame_width_1, frame_fill_1)
main = ImageOps.expand(main, frame_width_2, frame_fill_2)
w, h = main.size
h_up = expand_proc*(h//100)
im = Image.new("RGB", (w+(h_up*2), h+h_up), main_fill)
im.paste(main, (h_up, h_up))
return im
async def _draw_text(
text,
font_bytes,
font_size,
font_add = 20,
main_fill = (0, 0, 0),
text_fill = (255, 255, 255),
text_align = "center"
):
font = ImageFont.truetype(io.BytesIO(font_bytes), font_size)
w_txt, h_txt = ImageDraw.Draw(Image.new("RGB", (1, 1))).multiline_textsize(text=text, font=font)
txt = Image.new("RGB", (w_txt, h_txt+font_add), main_fill)
ImageDraw.Draw(txt).text((0, 0), text=text, font=font, fill=text_fill, align=text_align)
return txt
async def text_joiner(text_img_1, text_img_2, main_fill = (0, 0, 0)):
w_txt_1, h_txt_1 = text_img_1.size
w_txt_2, h_txt_2 = text_img_2.size
w = max(w_txt_1, w_txt_2)
h = h_txt_1 + h_txt_2
text = Image.new("RGB", (w, h), main_fill)
text.paste(text_img_1, ((w-w_txt_1)//2, 0))
text.paste(text_img_2, ((w-w_txt_2)//2, h_txt_1))
return text
async def draw_text(text, font_bytes, font_size):
text = await textwrap(text)
if len(text) == 1:
text = await _draw_text(text[0], font_bytes, font_size[0] )
else:
text_img_1 = await _draw_text(text[ 0], font_bytes, font_size[0])
text_img_2 = await _draw_text(text[-1], font_bytes, font_size[1])
text = await text_joiner(text_img_1, text_img_2)
return text
async def text_finaller(text, main, expand_width_proc = 25, main_fill = (0, 0, 0)):
x = min(main.size)
w_txt, h_txt = text.size
w_proc = expand_width_proc*(w_txt//100)
h_proc = expand_width_proc*(h_txt//100)
back = Image.new("RGB", (w_txt+(w_proc*2), h_txt+(h_proc*2)), main_fill)
back.paste(text, (w_proc, h_proc))
back.thumbnail((x, x))
return back
async def joiner(text_img, main_img, format_save="JPEG"):
w_im, h_im = main_img.size
w_txt, h_txt = text_img.size
text_img.thumbnail((min(w_im, h_im), min(w_im, h_im)))
w_txt, h_txt = text_img.size
main_img = main_img.crop((0, 0, w_im, h_im+h_txt))
main_img.paste(text_img, ((w_im-w_txt)//2, h_im))
output = io.BytesIO()
main_img.save(output, format_save)
output.seek(0)
return output.getvalue()
async def demotion(font_bytes, bytes_image, text, type):
main = await draw_main(bytes_image, type)
font_size = [20*(min(main.size)//100), 15*(min(main.size)//100)]
text = await draw_text(text, font_bytes, font_size)
text = await text_finaller(text, main)
output = await joiner(text, main)
return output
font_bytes = requests.get("https://raw.githubusercontent.com/KeyZenD/l/master/times.ttf").content
#######################