-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpic2pack.py
More file actions
125 lines (109 loc) · 3.86 KB
/
pic2pack.py
File metadata and controls
125 lines (109 loc) · 3.86 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
from telethon import events
from telethon.errors.rpcerrorlist import YouBlockedUserError
from .. import loader, utils
import string
import random
from PIL import Image
import io
from asyncio import sleep
def register(cb):
cb(pic2packMod())
class pic2packMod(loader.Module):
"""pic2pack"""
strings = {'name': 'pic2pack'}
def __init__(self):
self.name = self.strings['name']
self._me = None
self._ratelimit = []
async def client_ready(self, client, db):
self._db = db
self._client = client
self.me = await client.get_me()
async def pic2packcmd(self, message):
""".pic2pack {packname} + <reply to photo>"""
reply = await message.get_reply_message()
if not reply:
await message.edit("<b>Reply to photo❗</b>")
return
args = utils.get_args_raw(message)
if not args:
await message.edit("<b>Packname</b>❓")
return
chat = '@Stickers'
name = "".join([random.choice(list(string.ascii_lowercase+string.ascii_uppercase)) for _ in range(16)])
emoji = "▫️"
image = io.BytesIO()
await message.client.download_file(reply, image)
image = Image.open(image)
w, h = image.size
www = max(w, h)
await message.edit("🔪<b>Cropping...</b>")
img = Image.new("RGBA", (www,www), (0,0,0,0))
img.paste(image, ((www-w)//2, 0))
face = img.resize((100,100))
fface = io.BytesIO()
fface.name = name+".png"
images = await cropping(img)
face.save(fface)
fface.seek(0)
await message.edit("<b>📤Uploading...</b>")
async with message.client.conversation(chat) as conv:
try:
x = await message.client.send_message(chat, "/cancel")
await (await conv.wait_event(events.NewMessage(incoming=True, from_users=chat))).delete()
await x.delete()
x = await message.client.send_message(chat, "/newpack")
await (await conv.wait_event(events.NewMessage(incoming=True, from_users=chat))).delete()
await x.delete()
x = await message.client.send_message(chat, args)
await (await conv.wait_event(events.NewMessage(incoming=True, from_users=chat))).delete()
await x.delete()
for im in images:
blank = io.BytesIO(im)
blank.name = name+".png"
blank.seek(0)
x = await message.client.send_file(chat, blank, force_document=True)
await (await conv.wait_event(events.NewMessage(incoming=True, from_users=chat))).delete()
await x.delete()
x = await message.client.send_message(chat, emoji)
await (await conv.wait_event(events.NewMessage(incoming=True, from_users=chat))).delete()
await x.delete()
x = await message.client.send_message(chat, "/publish")
await (await conv.wait_event(events.NewMessage(incoming=True, from_users=chat))).delete()
await x.delete()
x = await message.client.send_file(chat, fface, force_document=True)
await (await conv.wait_event(events.NewMessage(incoming=True, from_users=chat))).delete()
await x.delete()
x = await message.client.send_message(chat, name)
ending = await conv.wait_event(events.NewMessage(incoming=True, from_users=chat))
await x.delete()
await ending.delete()
for part in ending.raw_text.split():
if part.startswith("https://t.me/"):
break
await message.edit('✅<b>Uploaded successful!</b>\n'+part)
except YouBlockedUserError:
await message.edit('<b>@Stickers BLOCKED⛔</b>')
return
async def cropping(img):
(x, y) = img.size
cy = 5
cx = 5
sx = x//cx
sy = y//cy
if (sx*cx, sy*cy) != (x, y):
img = img.resize((sx*cx, sy*cy))
(lx, ly) = (0, 0)
media = []
for i in range(1, cy+1):
for o in range(1, cx+1):
mimg = img.crop((lx, ly, lx+sx, ly+sy))
mimg = mimg.resize((512,512))
bio = io.BytesIO()
bio.name = 'image.png'
mimg.save(bio, 'PNG')
media.append(bio.getvalue())
lx = lx + sx
lx = 0
ly = ly + sy
return media