Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.79 KB | None | 0 0
  1. import telebot
  2. from PIL import Image, ImageFilter
  3. import requests, json
  4. from random import randint
  5. token = '612819560:AAGQvoTJvj27axZdza1CQNUUi5a-ir-tOgA'
  6. bot = telebot.TeleBot(token=token)
  7.  
  8. def ramka_filter(path):
  9. img = Image.open(path)
  10. ramka = Image.open('ramka.png')
  11. ramka = ramka.resize((img.width, img.height))
  12. img.paste(ramka, (0, 0), ramka)
  13. return img
  14.  
  15. def blue_filter(path):
  16. img = Image.open(path).convert("RGB")
  17. pixels = img.load()
  18. for i in range(img.width):
  19. for j in range(img.height):
  20. r, g, b = pixels[i, j]
  21. b = min(b + 80, 255)
  22. pixels[i, j] = (r, g, b)
  23. return img
  24.  
  25. def face_id(path):
  26. file = open(path, "rb")
  27. content = file.read()
  28.  
  29. params = {
  30. 'returnFaceId': 'true',
  31. 'returnFaceLandmarks': 'false',
  32. 'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,' +
  33. 'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
  34. }
  35.  
  36. result = requests.post("https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect", data=content,
  37. params=params,
  38. headers={"Content-Type": "application/octet-stream",
  39. "Ocp-Apim-Subscription-Key": "d2b7961c349c4340b9a14a7281c164b1"})
  40. face_result = json.loads(result.text)
  41.  
  42. family = Image.open(path).convert('RGB')
  43.  
  44. print(face_result[0])
  45.  
  46. for face in face_result:
  47. rectangle = face['faceRectangle']
  48. x = rectangle['left']
  49. y = rectangle['top']
  50. w = rectangle['width']
  51. h = rectangle['height']
  52. box = (x, y, (x + w), (y + h))
  53. emotions = list(face['faceAttributes']['emotion'].items())
  54. emotions.sort(reverse=True, key=lambda x: x[1])
  55. if emotions[0][0] == 'happiness':
  56. mask = Image.open("4948.png")
  57. m = mask.resize((w, h))
  58. family.paste(m, box, m)
  59. elif emotions[0][0] == 'sadness':
  60. mask = Image.open("sad.png")
  61. m = mask.resize((w, h))
  62. family.paste(m, box, m)
  63. elif emotions[0][0] == 'anger':
  64. mask = Image.open("angry.png")
  65. m = mask.resize((w, h))
  66. family.paste(m, box, m)
  67. elif emotions[0][0] == 'contempt':
  68. mask = Image.open("contempt.png")
  69. m = mask.resize((w, h))
  70. family.paste(m, box, m)
  71. elif emotions[0][0] == 'disgust':
  72. mask = Image.open("disgust.png")
  73. m = mask.resize((w, h))
  74. family.paste(m, box, m)
  75. elif emotions[0][0] == 'fear':
  76. mask = Image.open("fear.png")
  77. m = mask.resize((w, h))
  78. family.paste(m, box, m)
  79. elif emotions[0][0] == 'neutral':
  80. mask = Image.open("neutral.png")
  81. m = mask.resize((w, h))
  82. family.paste(m, box, m)
  83. elif emotions[0][0] == 'surprise':
  84. mask = Image.open("surprise.png")
  85. m = mask.resize((w, h))
  86. family.paste(m, box, m)
  87. return family
  88. def black_white_filter(path):
  89. img = Image.open(path).convert('RGB')
  90. pixels = img.load()
  91. for i in range(img.width):
  92. for j in range(img.height):
  93. r, g, b = pixels[i, j]
  94. n = (r + g + b) // 3
  95. pixels[i, j] = (r, r, r)
  96. return img
  97.  
  98. def red_filter(path):
  99. img = Image.open(path).convert('RGB')
  100. pixels = img.load()
  101. for i in range(img.width):
  102. for j in range(img.height):
  103. r, g, b = pixels[i, j]
  104. r = min(r + 40, 800)
  105. pixels[i, j] = (r, g, b)
  106. return img
  107.  
  108. def x3_filter(path):
  109. img = Image.open(path).convert("RGB")
  110. pixels = img.load()
  111.  
  112. for i in range(img.width):
  113. for j in range(img.height):
  114. r, g, b = pixels[i, j]
  115. a = randint(-50, 255)
  116. r = max(min(r + a, 255), 0)
  117. a = randint(-50, 50)
  118. g = max(min(g + a, 255), 0)
  119. a = randint(-50, 50)
  120. b = max(min(b + a, 255), 0)
  121. pixels[i, j] = (r, g, b, a)
  122. return img
  123.  
  124. def nedoramka_filter(path):
  125. def paint_blue(im):
  126. pixels = im.load()
  127. for i in range(im.width):
  128. for j in range(im.height):
  129. r, g, b = pixels[i, j]
  130. b = min(b + 100, 255)
  131. pixels[i, j] = (r, g, b)
  132.  
  133. def paint(im):
  134. pixels = im.load()
  135. for i in range(im.width):
  136. for j in range(im.height):
  137. r, g, b = pixels[i, j]
  138. b = min(b + 150, 255)
  139. pixels[i, j] = (r, g, b)
  140.  
  141. source = Image.open(path)
  142. result = source.copy()
  143. x = result.width // 9
  144. y = result.height // 9
  145. box = (x, y, source.width - x, source.height - y)
  146. square = source.crop(box)
  147. paint(square)
  148. paint_blue(result)
  149. result.paste(square, box)
  150.  
  151. result = result.filter(ImageFilter.BLUR)
  152.  
  153. x = 2 * result.width // 9
  154. y = 2 * result.height // 9
  155. box = (x, y, source.width - x, source.height - y)
  156. square = source.crop(box)
  157. result.paste(square, box)
  158. return result
  159.  
  160. def green_filter(path):
  161. img = Image.open(path).convert('RGB')
  162. pixels = img.load()
  163. for i in range(img.width):
  164. for j in range(img.height):
  165. r, g, b = pixels[i, j]
  166. g = min(g + 40, 1000)
  167. pixels[i, j] = (r, g, b)
  168. return img
  169.  
  170. def red_filter(path):
  171. img = Image.open(path).convert('RGB')
  172. pixels = img.load()
  173. for i in range(img.width):
  174. for j in range(img.height):
  175. r, g, b = pixels[i, j]
  176. r = min(r + 40, 255)
  177. pixels[i, j] = (r, g, b)
  178. return img
  179.  
  180. def kek_filter(path):
  181. img = Image.open(path)
  182.  
  183. box = (0, 0, img.width // 2, img.height)
  184. left = img.crop(box)
  185. left = left.transpose(Image.FLIP_LEFT_RIGHT)
  186.  
  187. img.paste(left, (img.width // 2, 0))
  188.  
  189. return img
  190.  
  191. keyboard = telebot.types.ReplyKeyboardMarkup()
  192. one_photoshop = telebot.types.KeyboardButton('Cделать фото холодным')
  193. two_photoshop = telebot.types.KeyboardButton('Черно-белое фото')
  194. three_photoshop = telebot.types.KeyboardButton('Сделать фото зеленым')
  195. four_photoshop = telebot.types.KeyboardButton('Сделать фото тёплым')
  196. six_photoshop = telebot.types.KeyboardButton('Сделать рамку')
  197. seven_photoshop = telebot.types.KeyboardButton('Сделать недорамку')
  198. eight_photoshop = telebot.types.KeyboardButton('Сделать шумы на фото')
  199. nine_photoshop = telebot.types.KeyboardButton('Сделать кек')
  200. ten_photoshop = telebot.types.KeyboardButton('Изменение лиц на эмоджи')
  201. keyboard.row(one_photoshop, two_photoshop, three_photoshop)
  202. keyboard.row(four_photoshop, six_photoshop)
  203. keyboard.row(seven_photoshop, eight_photoshop, nine_photoshop)
  204. keyboard.row(ten_photoshop)
  205. @bot.message_handler(commands=['start'])
  206. def start(message):
  207. bot.send_message(message.chat.id, 'Привет, я могу редактировать твое фото. Выбери фильтр и отправь любое фото', reply_markup= keyboard)
  208. print(message.text)
  209. bot.register_next_step_handler(message, choose)
  210. @bot.message_handler(commands=['info'])
  211. def start(message):
  212. bot.send_message(message.chat.id, 'Бот был сделан в 2018г. Разработчики: Русяев Марк, Алиуллин Амир, Камалдинов Мансур')
  213.  
  214. def choose(message):
  215. if message.text == 'Cделать фото холодным':
  216. bot.send_message(message.chat.id, 'Отправь мне фотографию')
  217. bot.register_next_step_handler(message, filter1)
  218. if message.text == 'Черно-белое фото':
  219. bot.send_message(message.chat.id, 'Отправь мне фотографию')
  220. bot.register_next_step_handler(message, filter2)
  221. if message.text == 'Сделать фото зеленым':
  222. bot.send_message(message.chat.id, 'Отправь мне фотографию')
  223. bot.register_next_step_handler(message, filter3)
  224. if message.text == 'Сделать фото тёплым':
  225. bot.send_message(message.chat.id, 'Отправь мне фотографию')
  226. bot.register_next_step_handler(message, filter4)
  227. if message.text == 'Сделать рамку':
  228. bot.send_message(message.chat.id, 'Отправь мне фотографию')
  229. bot.register_next_step_handler(message, filter6)
  230. if message.text == 'Сделать недорамку':
  231. bot.send_message(message.chat.id, 'Отправь мне фотографию')
  232. bot.register_next_step_handler(message, filter7)
  233. if message.text == 'Сделать шумы на фото':
  234. bot.send_message(message.chat.id, 'Отправь мне фотографию')
  235. bot.register_next_step_handler(message, filter8)
  236. if message.text == 'Сделать кек':
  237. bot.send_message(message.chat.id, 'Отправь мне фотографию')
  238. bot.register_next_step_handler(message, filter9)
  239. if message.text == 'Изменение лиц на эмоджи':
  240. bot.send_message(message.chat.id, 'Отправь мне фотографию')
  241. bot.register_next_step_handler(message, filter10)
  242.  
  243. def filter1(message):
  244. bot.send_message(message.chat.id, 'А вот и фоточка')
  245. print(message.photo)
  246. raw = message.photo[-1].file_id
  247. path = raw + ".jpg"
  248. file_info = bot.get_file(raw)
  249. downloaded_file = bot.download_file(file_info.file_path)
  250. with open(path, 'wb') as new_file:
  251. new_file.write(downloaded_file)
  252. img = blue_filter(path)
  253. img.save(path)
  254. bot.send_photo(message.chat.id, open(path, 'rb'))
  255. bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
  256. bot.register_next_step_handler(message, choose)
  257.  
  258. def filter10(message):
  259. bot.send_message(message.chat.id, 'А вот и фоточка')
  260. print(message.photo)
  261. raw = message.photo[-1].file_id
  262. path = raw + ".jpg"
  263. file_info = bot.get_file(raw)
  264. downloaded_file = bot.download_file(file_info.file_path)
  265. with open(path, 'wb') as new_file:
  266. new_file.write(downloaded_file)
  267. img = face_id(path)
  268. img.save(path)
  269. bot.send_photo(message.chat.id, open(path, 'rb'))
  270. bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
  271. bot.register_next_step_handler(message, choose)
  272.  
  273. def filter2(message):
  274. bot.send_message(message.chat.id, 'А вот и фоточка')
  275. print(message.photo)
  276. raw = message.photo[-1].file_id
  277. path = raw + ".jpg"
  278. file_info = bot.get_file(raw)
  279. downloaded_file = bot.download_file(file_info.file_path)
  280. with open(path, 'wb') as new_file:
  281. new_file.write(downloaded_file)
  282. img = black_white_filter(path)
  283. img.save(path)
  284. bot.send_photo(message.chat.id, open(path, 'rb'))
  285. bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
  286. bot.register_next_step_handler(message, choose)
  287.  
  288. def filter8(message):
  289. bot.send_message(message.chat.id, 'А вот и фоточка')
  290. print(message.photo)
  291. raw = message.photo[-1].file_id
  292. path = raw + ".jpg"
  293. file_info = bot.get_file(raw)
  294. downloaded_file = bot.download_file(file_info.file_path)
  295. with open(path, 'wb') as new_file:
  296. new_file.write(downloaded_file)
  297. img = x3_filter(path)
  298. img.save(path)
  299. bot.send_photo(message.chat.id, open(path, 'rb'))
  300. bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
  301. bot.register_next_step_handler(message, choose)
  302.  
  303. def filter7(message):
  304. bot.send_message(message.chat.id, 'А вот и фоточка')
  305. print(message.photo)
  306. raw = message.photo[-1].file_id
  307. path = raw + ".jpg"
  308. file_info = bot.get_file(raw)
  309. downloaded_file = bot.download_file(file_info.file_path)
  310. with open(path, 'wb') as new_file:
  311. new_file.write(downloaded_file)
  312. img = nedoramka_filter(path)
  313. img.save(path)
  314. bot.send_photo(message.chat.id, open(path, 'rb'))
  315. bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
  316. bot.register_next_step_handler(message, choose)
  317.  
  318. def filter3(message):
  319. bot.send_message(message.chat.id, 'А вот и фоточка')
  320. print(message.photo)
  321. raw = message.photo[-1].file_id
  322. path = raw + ".jpg"
  323. file_info = bot.get_file(raw)
  324. downloaded_file = bot.download_file(file_info.file_path)
  325. with open(path, 'wb') as new_file:
  326. new_file.write(downloaded_file)
  327. img = green_filter(path)
  328. img.save(path)
  329. bot.send_photo(message.chat.id, open(path, 'rb'))
  330. bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
  331. bot.register_next_step_handler(message, choose)
  332.  
  333. def filter4(message):
  334. bot.send_message(message.chat.id, 'А вот и фоточка')
  335. print(message.photo)
  336. raw = message.photo[-1].file_id
  337. path = raw + ".jpg"
  338. file_info = bot.get_file(raw)
  339. downloaded_file = bot.download_file(file_info.file_path)
  340. with open(path, 'wb') as new_file:
  341. new_file.write(downloaded_file)
  342. img = red_filter(path)
  343. img.save(path)
  344. bot.send_photo(message.chat.id, open(path, 'rb'))
  345. bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
  346. bot.register_next_step_handler(message, choose)
  347.  
  348. def filter9(message):
  349. bot.send_message(message.chat.id, 'А вот и фоточка')
  350. print(message.photo)
  351. raw = message.photo[-1].file_id
  352. path = raw + ".jpg"
  353. file_info = bot.get_file(raw)
  354. downloaded_file = bot.download_file(file_info.file_path)
  355. with open(path, 'wb') as new_file:
  356. new_file.write(downloaded_file)
  357. img = kek_filter(path)
  358. img.save(path)
  359. bot.send_photo(message.chat.id, open(path, 'rb'))
  360. bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
  361. bot.register_next_step_handler(message, choose)
  362.  
  363. def filter6(message):
  364. bot.send_message(message.chat.id, 'А вот и фоточка')
  365. print(message.photo)
  366. raw = message.photo[-1].file_id
  367. path = raw + ".jpg"
  368. file_info = bot.get_file(raw)
  369. downloaded_file = bot.download_file(file_info.file_path)
  370. with open(path, 'wb') as new_file:
  371. new_file.write(downloaded_file)
  372. img = ramka_filter(path)
  373. img.save(path)
  374. bot.send_photo(message.chat.id, open(path, 'rb'))
  375. bot.send_message(message.chat.id, 'Теперь выбери новый фильтр!')
  376. bot.register_next_step_handler(message, choose)
  377.  
  378. bot.polling(none_stop=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement