def __upload_to_imgur(path, caption): log_debug('__upload started') if not isfile(path): log_warn('File to be uploaded not found') return # TODO: Convert to GIF and upload. if path[-3:] == 'mp4': remove(path) log_warn('Skipping mp4 upload') return log_debug('Authorizing imgur client') im = Imgur(environ.get('IMGUR_CLIENT_ID'), environ.get('IMGUR_CLIENT_KEY'), environ.get('IMGUR_ACCESS_TOKEN'), environ.get('IMGUR_REFRESH_TOKEN')) for _ in range(5): try: im.upload_image(path=abspath(path), title=caption, album=environ.get('IMGUR_ALBUM')) log_debug('Image successfully uploaded') break except Exception: log_warn('Upload failed, refreshing token') im.refresh_access_token() sleep(10) continue else: log_error('Upload failed, proceeding') log_debug('Deleting file') remove(path) return
def __add_bulges_helper(img, number_of_cycles, bulge_probability): w, h = img.width - 1, img.height - 1 if w * h > 9000000: return img img_data = array(img) for _ in range(number_of_cycles): if random(1)[0] > bulge_probability: continue # (img, coords, radius, flatness, h, ior) img_data = __add_bulges( img_data, array([w, h]), array([int(w * random(1)), int(h * random(1))]), int(((img.width + img.height) / 10) * (random(1)[0] + 1)), 1 + random(3)[0], 6 + random(2)[0], 1.2 + random(2)[0]) log_debug('Bulge generated') return Image.fromarray(img_data)
def fry_gif(update, url, number_of_cycles, args): log_debug('Starting GIF Fry') number_of_emojis = (1.5 if args['high-fat'] else 1 if args['low-fat'] else 0) bulge_probability = (0.3 if args['heavy'] else 0.15 if args['light'] else 0) magnitude = (4 if args['deep'] else 1 if args['shallow'] else 2) name = update.message.from_user.first_name filename = '%s_%s_%s' % (update.message.chat_id, name, update.message.message_id) filepath = f'{bin_path}/temp/{filename}' caption = __get_caption(name, number_of_cycles, args) output = f'{bin_path}/temp/out_{filename}.mp4' gif_bio = BytesIO() gif_bio.name = f'{filename}.gif' if not __download_gif(url, filepath): log_error('GIF download failed') return log_debug('GIF successfully downloaded') fvs = FileVideoStream(f'{filepath}.mp4').start() frame = fvs.read() height, width, _ = frame.shape try: fps = fvs.get(CAP_PROP_FPS) log_debug(f'Detected FPS: {fps}') except: log_warn('FPS Detection failed, defaulting to 30.') fps = 30 out = VideoWriter(output, VideoWriter_fourcc(*'mp4v'), fps, (width, height)) fs = [__posterize, __sharpen, __increase_contrast, __colorize] shuffle(fs) log_debug(f'Frying first frame') out.write( fry_frame(frame, number_of_cycles, fs, number_of_emojis, bulge_probability, magnitude, args)) i = 2 while fvs.more() or fvs.more(): try: log_debug(f'Frying frame {i}') temp = fry_frame(fvs.read(), number_of_cycles, fs, number_of_emojis, bulge_probability, magnitude, args) out.write(temp) log_debug(f'Frame {i} fried successfully') i += 1 except Exception: log_error(f'Encountered error while frying frame {i}') break log_debug(f'All frames fried.') fvs.stop() fvs.stream.release() out.release() update.message.reply_animation(open(output, 'rb'), caption=caption, quote=True) log_debug(f'GIF saved and replied') try: __upload_to_imgur(output, caption) except (Exception, BaseException) as e: print(e) try: remove(f'{filepath}.mp4') except: pass log_debug('Image frying process completed')
def __fry(img, number_of_cycles, number_of_emojis, bulge_probability, laser, vitamin_b): log_debug('__fry starting') if laser: log_debug('Finding eye coordinates') coords = find_eyes(img) if coords: log_debug('Eye coordinates found') img = __add_lasers(img, coords) log_debug('Laser eyes added') else: log_debug('No eye coordinates found') if vitamin_b: log_debug('Finding char coordinates') coords = find_chars(img) if coords: log_debug('Char coordinates found') img = __add_b(img, coords, number_of_emojis / 20) log_debug('"B"s added') else: log_debug('No char coordinates found') log_debug('Adding emojis') img = __add_emojis(img, number_of_cycles * number_of_emojis) log_debug('emojis added') log_debug('Adding bulges') img = __add_bulges_helper(img, number_of_cycles, bulge_probability) log_debug('Bulges added, __fry completed') return img
def fry_image(update, url, number_of_cycles, args): log_debug('Starting Image Fry') number_of_emojis = (3 if args['high-fat'] else 1 if args['low-fat'] else 0 if args['no-fat'] else 2) bulge_probability = (0.75 if args['heavy'] else 0 if args['light'] else 0.45) magnitude = (4 if args['deep'] else 1 if args['shallow'] else 2) bio = BytesIO() name = update.message.from_user.first_name bio.name = filename = '%s_%s_%s.png' % (update.message.chat_id, name, update.message.message_id) filepath = f'{bin_path}/temp/{filename}' caption = __get_caption(name, number_of_cycles, args) success, img = __get_image(url) if not success: log_error('Image download failed') return log_debug('Image successfully downloaded') img = __fry(img, number_of_cycles, number_of_emojis, bulge_probability, not args['no-chilli'], args['vitamin-b']) log_debug('Frying effects starting') fs = [__posterize, __sharpen, __increase_contrast, __colorize] for _ in range(number_of_cycles): shuffle(fs) for f in fs: img = f(img, magnitude) log_debug('Frying effects applied') img.save(bio, 'PNG') bio.seek(0) update.message.reply_photo(bio, caption=caption, quote=True) log_debug('Image saved and replied') img.save(filepath, 'PNG') __upload_to_imgur(filepath, caption) log_debug('Image frying process completed')