| @@ -3,12 +3,11 @@ from PIL import Image, ImageDraw, ImageFont | |||||
| def sub(p1, p2): | def sub(p1, p2): | ||||
| return (p1[0] - p2[0], p1[1] - p2[1]) | return (p1[0] - p2[0], p1[1] - p2[1]) | ||||
| def overlay(image_in): | |||||
| def overlay(image_in, message="YOU DIED"): | |||||
| image = image_in.convert("RGBA") | image = image_in.convert("RGBA") | ||||
| size = image.size | size = image.size | ||||
| center = (size[0]/2, size[1]/2) | center = (size[0]/2, size[1]/2) | ||||
| message = "YOU DIED" | |||||
| limit = size[1] | limit = size[1] | ||||
| if size[0] / size[1] < 1: | if size[0] / size[1] < 1: | ||||
| limit = int(limit * size[0] / size[1]) | limit = int(limit * size[0] / size[1]) | ||||
| @@ -34,6 +33,14 @@ def overlay(image_in): | |||||
| draw.line([0, rect_bot[1] + y, size[0], rect_bot[1] + y], fill=(0,0,0,c)) | draw.line([0, rect_bot[1] + y, size[0], rect_bot[1] + y], fill=(0,0,0,c)) | ||||
| text_size = draw.textsize(message, font=font) | text_size = draw.textsize(message, font=font) | ||||
| # in case the text is too long | |||||
| ratio = text_size[0] / size[0] | |||||
| if ratio > 1: | |||||
| font = ImageFont.truetype("OptimusPrinceps.ttf", int(font_size / ratio)) | |||||
| text_size = draw.textsize(message, font=font) | |||||
| offset = (center[0] - text_size[0]/2, center[1] - text_size[1]/2) | offset = (center[0] - text_size[0]/2, center[1] - text_size[1]/2) | ||||
| draw.text(offset, message, (200,25,25), font=font) | draw.text(offset, message, (200,25,25), font=font) | ||||
| @@ -13,10 +13,13 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s | |||||
| def overlay_photo(update: Update, context: CallbackContext): | def overlay_photo(update: Update, context: CallbackContext): | ||||
| try: | try: | ||||
| caption = update.message.caption | |||||
| if not caption: | |||||
| caption = "YOU DIED" | |||||
| file = context.bot.getFile(update.message.photo[-1].file_id) | file = context.bot.getFile(update.message.photo[-1].file_id) | ||||
| bytes = file.download_as_bytearray() | bytes = file.download_as_bytearray() | ||||
| image = Image.open(io.BytesIO(bytes)) | image = Image.open(io.BytesIO(bytes)) | ||||
| overlayed = overlay(image).convert("RGB") | |||||
| overlayed = overlay(image, caption).convert("RGB") | |||||
| output = io.BytesIO() | output = io.BytesIO() | ||||
| overlayed.save(output, "JPEG") | overlayed.save(output, "JPEG") | ||||
| output.seek(0) | output.seek(0) | ||||
| @@ -26,10 +29,13 @@ def overlay_photo(update: Update, context: CallbackContext): | |||||
| def overlay_document(update: Update, context: CallbackContext): | def overlay_document(update: Update, context: CallbackContext): | ||||
| try: | try: | ||||
| caption = update.message.caption | |||||
| if not caption: | |||||
| caption = "YOU DIED" | |||||
| file = context.bot.getFile(update.message.document.file_id) | file = context.bot.getFile(update.message.document.file_id) | ||||
| bytes = file.download_as_bytearray() | bytes = file.download_as_bytearray() | ||||
| image = Image.open(io.BytesIO(bytes)) | image = Image.open(io.BytesIO(bytes)) | ||||
| overlayed = overlay(image) | |||||
| overlayed = overlay(image, caption) | |||||
| output = io.BytesIO() | output = io.BytesIO() | ||||
| overlayed.save(output, "PNG") | overlayed.save(output, "PNG") | ||||
| output.seek(0) | output.seek(0) | ||||