Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import telebot
- from PIL import Image, ImageFilter
- import requests, json
- from random import randint
- token = '612819560:AAGQvoTJvj27axZdza1CQNUUi5a-ir-tOgA'
- bot = telebot.TeleBot(token=token)
- def ramka_filter(path):
- img = Image.open(path)
- ramka = Image.open('ramka.png')
- ramka = ramka.resize((img.width, img.height))
- img.paste(ramka, (0, 0), ramka)
- return img
- def blue_filter(path):
- img = Image.open(path).convert("RGB")
- pixels = img.load()
- for i in range(img.width):
- for j in range(img.height):
- r, g, b = pixels[i, j]
- b = min(b + 80, 255)
- pixels[i, j] = (r, g, b)
- return img
- def face_id(path):
- file = open(path, "rb")
- content = file.read()
- params = {
- 'returnFaceId': 'true',
- 'returnFaceLandmarks': 'false',
- 'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,' +
- 'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
- }
- result = requests.post("https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect", data=content,
- params=params,
- headers={"Content-Type": "application/octet-stream",
- "Ocp-Apim-Subscription-Key": "d2b7961c349c4340b9a14a7281c164b1"})
- face_result = json.loads(result.text)
- family = Image.open(path).convert('RGB')
- print(face_result[0])
- for face in face_result:
- rectangle = face['faceRectangle']
- x = rectangle['left']
- y = rectangle['top']
- w = rectangle['width']
- h = rectangle['height']
- box = (x, y, (x + w), (y + h))
- emotions = list(face['faceAttributes']['emotion'].items())
- emotions.sort(reverse=True, key=lambda x: x[1])
- if emotions[0][0] == 'happiness':
- mask = Image.open("4948.png")
- m = mask.resize((w, h))
- family.paste(m, box, m)
- elif emotions[0][0] == 'sadness':
- mask = Image.open("sad.png")
- m = mask.resize((w, h))
- family.paste(m, box, m)
- elif emotions[0][0] == 'anger':
- mask = Image.open("angry.png")
- m = mask.resize((w, h))
- family.paste(m, box, m)
- elif emotions[0][0] == 'contempt':
- mask = Image.open("contempt.png")
- m = mask.resize((w, h))
- family.paste(m, box, m)
- elif emotions[0][0] == 'disgust':
- mask = Image.open("disgust.png")
- m = mask.resize((w, h))
- family.paste(m, box, m)
- elif emotions[0][0] == 'fear':
- mask = Image.open("fear.png")
- m = mask.resize((w, h))
- family.paste(m, box, m)
- elif emotions[0][0] == 'neutral':
- mask = Image.open("neutral.png")
- m = mask.resize((w, h))
- family.paste(m, box, m)
- elif emotions[0][0] == 'surprise':
- mask = Image.open("surprise.png")
- m = mask.resize((w, h))
- family.paste(m, box, m)
- return family
- def black_white_filter(path):
- img = Image.open(path).convert('RGB')
- pixels = img.load()
- for i in range(img.width):
- for j in range(img.height):
- r, g, b = pixels[i, j]
- n = (r + g + b) // 3
- pixels[i, j] = (r, r, r)
- return img
- def red_filter(path):
- img = Image.open(path).convert('RGB')
- pixels = img.load()
- for i in range(img.width):
- for j in range(img.height):
- r, g, b = pixels[i, j]
- r = min(r + 40, 800)
- pixels[i, j] = (r, g, b)
- return img
- def x3_filter(path):
- img = Image.open(path).convert("RGB")
- pixels = img.load()
- for i in range(img.width):
- for j in range(img.height):
- r, g, b = pixels[i, j]
- a = randint(-50, 255)
- r = max(min(r + a, 255), 0)
- a = randint(-50, 50)
- g = max(min(g + a, 255), 0)
- a = randint(-50, 50)
- b = max(min(b + a, 255), 0)
- pixels[i, j] = (r, g, b, a)
- return img
- def nedoramka_filter(path):
- def paint_blue(im):
- pixels = im.load()
- for i in range(im.width):
- for j in range(im.height):
- r, g, b = pixels[i, j]
- b = min(b + 100, 255)
- pixels[i, j] = (r, g, b)
- def paint(im):
- pixels = im.load()
- for i in range(im.width):
- for j in range(im.height):
- r, g, b = pixels[i, j]
- b = min(b + 150, 255)
- pixels[i, j] = (r, g, b)
- source = Image.open(path)
- result = source.copy()
- x = result.width // 9
- y = result.height // 9
- box = (x, y, source.width - x, source.height - y)
- square = source.crop(box)
- paint(square)
- paint_blue(result)
- result.paste(square, box)
- result = result.filter(ImageFilter.BLUR)
- x = 2 * result.width // 9
- y = 2 * result.height // 9
- box = (x, y, source.width - x, source.height - y)
- square = source.crop(box)
- result.paste(square, box)
- return result
- def green_filter(path):
- img = Image.open(path).convert('RGB')
- pixels = img.load()
- for i in range(img.width):
- for j in range(img.height):
- r, g, b = pixels[i, j]
- g = min(g + 40, 1000)
- pixels[i, j] = (r, g, b)
- return img
- def red_filter(path):
- img = Image.open(path).convert('RGB')
- pixels = img.load()
- for i in range(img.width):
- for j in range(img.height):
- r, g, b = pixels[i, j]
- r = min(r + 40, 255)
- pixels[i, j] = (r, g, b)
- return img
- def kek_filter(path):
- img = Image.open(path)
- box = (0, 0, img.width // 2, img.height)
- left = img.crop(box)
- left = left.transpose(Image.FLIP_LEFT_RIGHT)
- img.paste(left, (img.width // 2, 0))
- return img
- keyboard = telebot.types.ReplyKeyboardMarkup()
- one_photoshop = telebot.types.KeyboardButton('Cделать фото холодным')
- two_photoshop = telebot.types.KeyboardButton('Черно-белое фото')
- three_photoshop = telebot.types.KeyboardButton('Сделать фото зеленым')
- four_photoshop = telebot.types.KeyboardButton('Сделать фото тёплым')
- six_photoshop = telebot.types.KeyboardButton('Сделать рамку')
- seven_photoshop = telebot.types.KeyboardButton('Сделать недорамку')
- eight_photoshop = telebot.types.KeyboardButton('Сделать шумы на фото')
- nine_photoshop = telebot.types.KeyboardButton('Сделать кек')
- ten_photoshop = telebot.types.KeyboardButton('Изменение лиц на эмоджи')
- keyboard.row(one_photoshop, two_photoshop, three_photoshop)
- keyboard.row(four_photoshop, six_photoshop)
- keyboard.row(seven_photoshop, eight_photoshop, nine_photoshop)
- keyboard.row(ten_photoshop)
- @bot.message_handler(commands=['start'])
- def start(message):
- bot.send_message(message.chat.id, 'Привет, я могу редактировать твое фото. Выбери фильтр и отправь любое фото', reply_markup= keyboard)
- print(message.text)
- bot.register_next_step_handler(message, choose)
- @bot.message_handler(commands=['info'])
- def start(message):
- bot.send_message(message.chat.id, 'Бот был сделан в 2018г. Разработчики: Русяев Марк, Алиуллин Амир, Камалдинов Мансур')
- def choose(message):
- if message.text == 'Cделать фото холодным':
- bot.send_message(message.chat.id, 'Отправь мне фотографию')
- bot.register_next_step_handler(message, filter1)
- if message.text == 'Черно-белое фото':
- bot.send_message(message.chat.id, 'Отправь мне фотографию')
- bot.register_next_step_handler(message, filter2)
- if message.text == 'Сделать фото зеленым':
- bot.send_message(message.chat.id, 'Отправь мне фотографию')
- bot.register_next_step_handler(message, filter3)
- if message.text == 'Сделать фото тёплым':
- bot.send_message(message.chat.id, 'Отправь мне фотографию')
- bot.register_next_step_handler(message, filter4)
- if message.text == 'Сделать рамку':
- bot.send_message(message.chat.id, 'Отправь мне фотографию')
- bot.register_next_step_handler(message, filter6)
- if message.text == 'Сделать недорамку':
- bot.send_message(message.chat.id, 'Отправь мне фотографию')
- bot.register_next_step_handler(message, filter7)
- if message.text == 'Сделать шумы на фото':
- bot.send_message(message.chat.id, 'Отправь мне фотографию')
- bot.register_next_step_handler(message, filter8)
- if message.text == 'Сделать кек':
- bot.send_message(message.chat.id, 'Отправь мне фотографию')
- bot.register_next_step_handler(message, filter9)
- if message.text == 'Изменение лиц на эмоджи':
- bot.send_message(message.chat.id, 'Отправь мне фотографию')
- bot.register_next_step_handler(message, filter10)
- def filter1(message):
- bot.send_message(message.chat.id, 'А вот и фоточка')
- print(message.photo)
- raw = message.photo[-1].file_id
- path = raw + ".jpg"
- file_info = bot.get_file(raw)
- downloaded_file = bot.download_file(file_info.file_path)
- with open(path, 'wb') as new_file:
- new_file.write(downloaded_file)
- img = blue_filter(path)
- img.save(path)
- bot.send_photo(message.chat.id, open(path, 'rb'))
- bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
- bot.register_next_step_handler(message, choose)
- def filter10(message):
- bot.send_message(message.chat.id, 'А вот и фоточка')
- print(message.photo)
- raw = message.photo[-1].file_id
- path = raw + ".jpg"
- file_info = bot.get_file(raw)
- downloaded_file = bot.download_file(file_info.file_path)
- with open(path, 'wb') as new_file:
- new_file.write(downloaded_file)
- img = face_id(path)
- img.save(path)
- bot.send_photo(message.chat.id, open(path, 'rb'))
- bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
- bot.register_next_step_handler(message, choose)
- def filter2(message):
- bot.send_message(message.chat.id, 'А вот и фоточка')
- print(message.photo)
- raw = message.photo[-1].file_id
- path = raw + ".jpg"
- file_info = bot.get_file(raw)
- downloaded_file = bot.download_file(file_info.file_path)
- with open(path, 'wb') as new_file:
- new_file.write(downloaded_file)
- img = black_white_filter(path)
- img.save(path)
- bot.send_photo(message.chat.id, open(path, 'rb'))
- bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
- bot.register_next_step_handler(message, choose)
- def filter8(message):
- bot.send_message(message.chat.id, 'А вот и фоточка')
- print(message.photo)
- raw = message.photo[-1].file_id
- path = raw + ".jpg"
- file_info = bot.get_file(raw)
- downloaded_file = bot.download_file(file_info.file_path)
- with open(path, 'wb') as new_file:
- new_file.write(downloaded_file)
- img = x3_filter(path)
- img.save(path)
- bot.send_photo(message.chat.id, open(path, 'rb'))
- bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
- bot.register_next_step_handler(message, choose)
- def filter7(message):
- bot.send_message(message.chat.id, 'А вот и фоточка')
- print(message.photo)
- raw = message.photo[-1].file_id
- path = raw + ".jpg"
- file_info = bot.get_file(raw)
- downloaded_file = bot.download_file(file_info.file_path)
- with open(path, 'wb') as new_file:
- new_file.write(downloaded_file)
- img = nedoramka_filter(path)
- img.save(path)
- bot.send_photo(message.chat.id, open(path, 'rb'))
- bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
- bot.register_next_step_handler(message, choose)
- def filter3(message):
- bot.send_message(message.chat.id, 'А вот и фоточка')
- print(message.photo)
- raw = message.photo[-1].file_id
- path = raw + ".jpg"
- file_info = bot.get_file(raw)
- downloaded_file = bot.download_file(file_info.file_path)
- with open(path, 'wb') as new_file:
- new_file.write(downloaded_file)
- img = green_filter(path)
- img.save(path)
- bot.send_photo(message.chat.id, open(path, 'rb'))
- bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
- bot.register_next_step_handler(message, choose)
- def filter4(message):
- bot.send_message(message.chat.id, 'А вот и фоточка')
- print(message.photo)
- raw = message.photo[-1].file_id
- path = raw + ".jpg"
- file_info = bot.get_file(raw)
- downloaded_file = bot.download_file(file_info.file_path)
- with open(path, 'wb') as new_file:
- new_file.write(downloaded_file)
- img = red_filter(path)
- img.save(path)
- bot.send_photo(message.chat.id, open(path, 'rb'))
- bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
- bot.register_next_step_handler(message, choose)
- def filter9(message):
- bot.send_message(message.chat.id, 'А вот и фоточка')
- print(message.photo)
- raw = message.photo[-1].file_id
- path = raw + ".jpg"
- file_info = bot.get_file(raw)
- downloaded_file = bot.download_file(file_info.file_path)
- with open(path, 'wb') as new_file:
- new_file.write(downloaded_file)
- img = kek_filter(path)
- img.save(path)
- bot.send_photo(message.chat.id, open(path, 'rb'))
- bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
- bot.register_next_step_handler(message, choose)
- def filter6(message):
- bot.send_message(message.chat.id, 'А вот и фоточка')
- print(message.photo)
- raw = message.photo[-1].file_id
- path = raw + ".jpg"
- file_info = bot.get_file(raw)
- downloaded_file = bot.download_file(file_info.file_path)
- with open(path, 'wb') as new_file:
- new_file.write(downloaded_file)
- img = ramka_filter(path)
- img.save(path)
- bot.send_photo(message.chat.id, open(path, 'rb'))
- bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
- bot.register_next_step_handler(message, choose)
- bot.polling(none_stop=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement