From 1c26dabad666321fed4a8382b674f3f9a91b9a04 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Sun, 14 Jun 2020 12:35:02 -0400 Subject: [PATCH] Allow for text to be provided in the caption --- overlay.py | 11 +++++++++-- you-died.py | 10 ++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/overlay.py b/overlay.py index 2326653..e9e0157 100644 --- a/overlay.py +++ b/overlay.py @@ -3,12 +3,11 @@ from PIL import Image, ImageDraw, ImageFont def sub(p1, p2): 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") size = image.size center = (size[0]/2, size[1]/2) - message = "YOU DIED" limit = size[1] if size[0] / size[1] < 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)) 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) draw.text(offset, message, (200,25,25), font=font) diff --git a/you-died.py b/you-died.py index 65f2b52..e31877c 100644 --- a/you-died.py +++ b/you-died.py @@ -13,10 +13,13 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s def overlay_photo(update: Update, context: CallbackContext): try: + caption = update.message.caption + if not caption: + caption = "YOU DIED" file = context.bot.getFile(update.message.photo[-1].file_id) bytes = file.download_as_bytearray() image = Image.open(io.BytesIO(bytes)) - overlayed = overlay(image).convert("RGB") + overlayed = overlay(image, caption).convert("RGB") output = io.BytesIO() overlayed.save(output, "JPEG") output.seek(0) @@ -26,10 +29,13 @@ def overlay_photo(update: Update, context: CallbackContext): def overlay_document(update: Update, context: CallbackContext): try: + caption = update.message.caption + if not caption: + caption = "YOU DIED" file = context.bot.getFile(update.message.document.file_id) bytes = file.download_as_bytearray() image = Image.open(io.BytesIO(bytes)) - overlayed = overlay(image) + overlayed = overlay(image, caption) output = io.BytesIO() overlayed.save(output, "PNG") output.seek(0)