├── README.md ├── fonts └── font.ttf ├── main.py ├── photo_editor.py ├── pic ├── paste │ └── ok.jpg └── template.jpg └── requirements.txt /README.md: -------------------------------------------------------------------------------- 1 | # DemotivatorBot 2 | телеграм бот, создающий демотиваторы из пользовательских картинок 3 | 4 | [![](https://img.shields.io/badge/github-WELISK-red?style=for-the-badge&logo=github)](https://github.com/welisk) 5 | 6 | ## Установка 7 | 8 | 9 | Установите [Python](https://python.org) 10 | ```bash 11 | # Скопируйте репозиторий 12 | git clone https://github.com/welisk/DemotivatorBot.git 13 | 14 | # Перейдите в папку проекта 15 | cd DemotivatorBot 16 | 17 | # Установите нужные библиотеки 18 | pip install -r requirements.txt 19 | 20 | # откройте файл main.py и вставьте ваш токен в константу TOKEN 21 | TOKEN = '' 22 | 23 | # Запустите бота 24 | python main.py 25 | ``` 26 | ![Screenshot](https://github.com/welisk/telegram-bot-demotivator/blob/main/pic/paste/ok.jpg) 27 | -------------------------------------------------------------------------------- /fonts/font.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzzlepick/DemotivatorBot/1602d2caff39fdcbab6a6999713c2cc09dda1b12/fonts/font.ttf -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from aiogram import Bot, Dispatcher, executor, types 2 | import photo_editor 3 | from colorama import init, Fore, Style 4 | 5 | TOKEN = '' 6 | 7 | init() 8 | 9 | try: 10 | bot = Bot(token= TOKEN) 11 | dp = Dispatcher(bot) 12 | 13 | @dp.message_handler(commands=['start']) 14 | async def start(message: types.Message): 15 | await bot.send_message(message.from_user.id, 'Отправь мне фото с подписью') 16 | 17 | @dp.message_handler(content_types=['photo']) 18 | async def photo(message): 19 | 20 | await message.photo[-1].download('pic/mem.jpg') 21 | 22 | if message.caption: 23 | text = message.caption 24 | 25 | photo_editor.add_text(text) 26 | await bot.send_photo(message.from_user.id, photo=open('pic/paste/ok.jpg', 'rb')) 27 | else: 28 | await bot.send_message(message.from_user.id, 'Ты забыл добавить подпись') 29 | 30 | if __name__ == '__main__': 31 | print('Бот успешно запущен') 32 | executor.start_polling(dp, skip_updates=True) 33 | except: 34 | print(Fore.RED + 'Вы забыли ввести токен\nЭто можно сделать в файле main.py' + Fore.RESET) 35 | -------------------------------------------------------------------------------- /photo_editor.py: -------------------------------------------------------------------------------- 1 | from PIL import ImageFont 2 | from PIL import Image 3 | from PIL import ImageDraw 4 | 5 | 6 | def add_text(text): 7 | template = Image.open('pic/template.jpg') 8 | mem = Image.open('pic/mem.jpg').convert('RGBA') 9 | 10 | width = 610 11 | height = 569 12 | resized_mem = mem.resize((width, height), Image.ANTIALIAS) 13 | 14 | text_position = (0, 0) 15 | text_color = (266,0,0) 16 | 17 | 18 | strip_width, strip_height = 700, 1300 19 | 20 | def findLen(text_len): 21 | counter = 0 22 | for i in text_len: 23 | counter += 1 24 | return counter 25 | 26 | font_width = 60 27 | 28 | if findLen(text) >= 25: 29 | font_width = 50 30 | 31 | background = Image.new('RGB', (strip_width, strip_height)) #creating the black strip 32 | draw = ImageDraw.Draw(template) 33 | 34 | if '\n' in text: 35 | split_offers = text.split('\n') 36 | 37 | for i in range(2): 38 | if i == 1: 39 | strip_height += 110 40 | font_width -= 20 41 | font = ImageFont.truetype("fonts/font.ttf", font_width) 42 | text_width, text_height = draw.textsize(split_offers[i], font) 43 | 44 | position = ((strip_width-text_width)/2,(strip_height-text_height)/2) 45 | draw.text(position, split_offers[i], font=font) 46 | else: 47 | font = ImageFont.truetype("fonts/font.ttf", font_width) 48 | text_width, text_height = draw.textsize(text, font) 49 | strip_height = 1330 50 | position = ((strip_width-text_width)/2,(strip_height-text_height)/2) 51 | draw.text(position, text, font=font) 52 | 53 | template.paste(resized_mem, (54, 32), resized_mem) 54 | template.save("pic/paste/ok.jpg") 55 | 56 | -------------------------------------------------------------------------------- /pic/paste/ok.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzzlepick/DemotivatorBot/1602d2caff39fdcbab6a6999713c2cc09dda1b12/pic/paste/ok.jpg -------------------------------------------------------------------------------- /pic/template.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muzzlepick/DemotivatorBot/1602d2caff39fdcbab6a6999713c2cc09dda1b12/pic/template.jpg -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | aiogram==2.13 2 | colorama==0.4.4 3 | Pillow==8.2.0 4 | --------------------------------------------------------------------------------