-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemes.py
192 lines (170 loc) · 7.82 KB
/
memes.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import PIL.Image
import PIL.ImageChops
from pyrogram import Client
from typing import BinaryIO
from sambot import Sambot, BotPipelineSegmentBase, MessageAdapter
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import PIL
import io
def LoadIntoSambot(bot: Sambot):
bot.AddPipelineSegment(Memes_Aliens())
bot.AddPipelineSegment(Memes_ToyStory())
bot.AddPipelineSegment(Memes_HumanDisaster())
bot.AddPipelineSegment(Memes_Simply())
bot.AddPipelineSegment(Memes_Detroit())
class Memes_Aliens(BotPipelineSegmentBase):
"""
History.com's Aliens guy
More : https://knowyourmeme.com/memes/ancient-aliens
"""
async def CanHandle(self, sambot: Sambot, message: MessageAdapter):
if not message.text: return
if not message.from_user.is_self: return
return message.text.split()[0] == '.aliens'
async def ProcessMessage(self, sambot: Sambot, bot: Client, message: MessageAdapter):
await message.delete()
image = _memeGenerator.HistoryAliensGuy(message.text[8:] if len(message.text) > 8 else "ALIENS")
await bot.send_photo(
chat_id=message.chat.id,
reply_to_message_id=message.reply_to_message_id or message.reply_to_top_message_id,
photo=image
)
class Memes_ToyStory(BotPipelineSegmentBase):
"""
The "one does not simply" meme from Lord of the Rings.
More : https://knowyourmeme.com/memes/one-does-not-simply-walk-into-mordor
"""
async def CanHandle(self, sambot: Sambot, message: MessageAdapter):
if not message.text: return
if not message.from_user.is_self: return
return message.text.split()[0] == '.toystory'
async def ProcessMessage(self, sambot: Sambot, bot: Client, message: MessageAdapter):
await message.delete()
image = _memeGenerator.ToyStoryMeme(message.text[9:] if len(message.text) > 9 else "MEMES")
await bot.send_photo(
chat_id=message.chat.id,
reply_to_message_id=message.reply_to_message_id or message.reply_to_top_message_id,
photo=image
)
class Memes_HumanDisaster(BotPipelineSegmentBase):
async def CanHandle(self, sambot: Sambot, message: MessageAdapter):
if not message.text: return
if not message.from_user.is_self: return
return message.text.split()[0] == '.hd'
async def ProcessMessage(self, sambot: Sambot, bot: Client, message: MessageAdapter):
await message.delete()
image = _memeGenerator.HumanDisaster(message.text[3:])
await bot.send_photo(
chat_id=message.chat.id,
reply_to_message_id=message.reply_to_message_id or message.reply_to_top_message_id,
photo=image
)
class Memes_Simply(BotPipelineSegmentBase):
async def CanHandle(self, sambot: Sambot, message: MessageAdapter):
if not message.text: return
if not message.from_user.is_self: return
return message.text.split()[0] == '.simply'
async def ProcessMessage(self, sambot: Sambot, bot: Client, message: MessageAdapter):
await message.delete()
image = _memeGenerator.OneDoesNotSimply(message.text[7:])
await bot.send_photo(
chat_id=message.chat.id,
reply_to_message_id=message.reply_to_message_id or message.reply_to_top_message_id,
photo=image
)
class Memes_Detroit(BotPipelineSegmentBase):
async def CanHandle(self, sambot: Sambot, message: MessageAdapter):
if not message.text: return
if not message.from_user.is_self: return
return message.text == '.detroit'
async def ProcessMessage(self, sambot: Sambot, bot: Client, message: MessageAdapter):
if not message.reply_to_message or not message.reply_to_message.media:
await message.edit_text(text="Sam, You need to reply to a message with an image")
return
await message.edit_text(text="Hold on")
dl_img = await bot.download_media(message.reply_to_message, in_memory=True)
return_img = _memeGenerator.DetroitMeme(dl_img)
await bot.send_photo(
chat_id=message.chat.id,
reply_to_message_id=message.reply_to_message_id or message.reply_to_top_message_id,
photo=return_img
)
await message.delete()
class _memeGenerator:
def _drawTextWithOutline(draw, text, x, y, fsize):
draw.text((x-2, y-2), text,(0,0,0),font=fsize)
draw.text((x+2, y-2), text,(0,0,0),font=fsize)
draw.text((x+2, y+2), text,(0,0,0),font=fsize)
draw.text((x-2, y+2), text,(0,0,0),font=fsize)
draw.text((x, y), text, (255,255,255), font=fsize)
return
def _drawText(draw, text, x, y, fsize):
draw.text((x, y), text, (255,255,255), font=fsize)
return
def _average_colour(image):
colour_tuple = [None, None, None]
for channel in range(3):
# Get data for one channel at a time
pixels = image.getdata(band=channel)
values = []
for pixel in pixels:
values.append(pixel)
colour_tuple[channel] = sum(values) / len(values)
return tuple(colour_tuple)
def OneDoesNotSimply(text) -> BinaryIO:
img = Image.open("static/one-does-not-simply.jpg")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("static/impact.ttf", 42)
w = draw.textlength(text, font)
_memeGenerator._drawTextWithOutline(draw,text, img.width/2 - w/2, 280, font)
output = io.BytesIO()
img.save(output, format='JPEG')
return output
def HumanDisaster(text) -> BinaryIO:
img = Image.open("static/human-disaster.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("static/impact.ttf", 42)
w = draw.textlength(text, font)
_memeGenerator._drawText(draw,text, img.width/2 - w/2, 570, font)
output = io.BytesIO()
img.save(output, format='JPEG')
return output
def HistoryAliensGuy(text) -> BinaryIO:
img = Image.open("static/history-aliens.jpg")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("static/impact.ttf", 50)
w = draw.textlength(text, font)
_memeGenerator._drawTextWithOutline(draw,text, img.width/2 - w/2, 377, font)
output = io.BytesIO()
img.save(output, format='JPEG')
return output
def ToyStoryMeme(text) -> BinaryIO:
img = Image.open("static/woody-buzz.jpg")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("static/impact.ttf", 45)
w = draw.textlength(text, font)
w1 = draw.textlength(text + " Everywhere", font)
_memeGenerator._drawTextWithOutline(draw,str(text).capitalize(), img.width/2 - w/2, 2, font)
_memeGenerator._drawTextWithOutline(draw,str(text).capitalize() + " Everywhere", img.width/2 - w1/2, 250, font)
output = io.BytesIO()
img.save(output, format='JPEG')
return output
def DetroitMeme(image:BinaryIO) -> BinaryIO:
img = Image.open(image)
img = Image.open("static/dbh-painting-meme.png")
background = Image.open(image)
size = (2560,1600)
small_image = (920,1150)
edited_image = background
edited_image = edited_image.resize(size,)
scaled_image = background.resize(small_image, PIL.Image.Resampling.LANCZOS)
edited_image = Image.new("RGBA", (2560,1600), "#F00")
scaled_image = scaled_image.convert('RGBA')
scaled_image = scaled_image.rotate(4, expand=1 ).resize(small_image)
edited_image.paste(scaled_image, (845, 248),scaled_image)
edited_image.paste(img, (0,0), img)
output = io.BytesIO()
edited_image.save(output, format='PNG')
return output