-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
57 lines (38 loc) · 1.61 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import logging
from colorization.colorize import colorize
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level = logging.INFO)
logger = logging.getLogger(__name__)
def start(update, context):
update.message.reply_text("Hi! Send black and white photo to me, I'll try to colorize it for you")
def main():
updater = Updater(os.getenv('BOT_TOKEN'), use_context = True)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start))
dp.add_handler(CommandHandler('help', start))
dp.add_handler(MessageHandler(Filters.photo, process))
dp.add_handler(MessageHandler(Filters.document.category('image'), process))
updater.start_polling()
updater.idle()
def process(update, context):
photo = update.message.photo[-1] if update.message.photo else update.message.document
photo_file = photo.get_file()
file_extension = f".{photo_file.file_path.split('.')[-1]}"
logger.info(photo_file.file_path)
filename = f"./photos/{photo.file_unique_id}{file_extension}"
colorized_filename = filename.replace(file_extension , f"_colorized{file_extension}")
photo_file.download(filename)
colorize(filename, colorized_filename)
colorized_photo = open(colorized_filename, 'rb')
update.message.reply_photo(colorized_photo)
colorized_photo.close()
os.remove(filename)
os.remove(colorized_filename)
if __name__ == '__main__':
main()