forked from rupansh/OpenDistortBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistortion_bot.py
62 lines (43 loc) · 1.77 KB
/
distortion_bot.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
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from io import BytesIO
from glob import glob
import os
from PIL import Image
import random
TOKEN = "ENTER YOUR TOKEN"
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
def distort(fname):
image = Image.open(fname)
imgdimens = image.width, image.height
distortcmd = f"magick {fname} -liquid-rescale 60x60%! -resize {imgdimens[0]}x{imgdimens[1]}\! result/{fname}"
os.system(distortcmd)
buf = BytesIO()
buf.name = 'image.jpeg'
image = Image.open(f"result/{fname}")
filetype = "JPEG" if fname.endswith(".jpg") else "PNG"
image.save(buf, filetype)
buf.seek(0)
return buf
@dp.message_handler(commands=['start', 'help'])
async def send_welcome(message: types.Message):
await message.reply("This is a bot based on the distortion bot(@DistortBot). Since its not open source, "
"I decided to make an open source verison")
@dp.message_handler(commands=['distort'])
async def dodistort(message):
for distorted in glob("distorted*"):
os.remove(distorted)
for findistorted in glob("*/distorted*"):
os.remove(findistorted)
if message.reply_to_message and (message.reply_to_message.photo or message.reply_to_message.sticker):
img = await message.reply_to_message.photo[-1].get_file() if message.reply_to_message.photo \
else await message.reply_to_message.sticker.get_file()
imgname = f"distorted{random.randint(1, 100)}"
imgname += ".jpg" if message.reply_to_message.photo \
else ".png"
await img.download(imgname)
await message.reply_photo(photo=distort(imgname), reply=message)
if __name__ == '__main__':
executor.start_polling(dp)