diff --git a/.dockerignore b/.dockerignore index 2eea525..9336073 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,3 @@ -.env \ No newline at end of file +.env +red_eb/ +venv/ \ No newline at end of file diff --git a/bot.py b/bot.py index 470c807..65227f3 100644 --- a/bot.py +++ b/bot.py @@ -2,7 +2,8 @@ from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters, InlineQueryHandler import logging -from render import render_text_on_image +from render import render_text_on_image, image_to_string +from io import BytesIO import os @@ -21,6 +22,20 @@ async def respond_with_picture(update: Update, context: ContextTypes.DEFAULT_TYP file = render_text_on_image("red_ebalo.png", update.message.text) await context.bot.send_sticker(chat_id=update.effective_chat.id, sticker=file) +async def image2string(update: Update, context: ContextTypes.DEFAULT_TYPE): + photo = update.message.photo[-1] + + + file = await photo.get_file() + bio = BytesIO() + await file.download_to_memory(out=bio) + + bio.seek(0) + + s = image_to_string(bio) + + await update.message.reply_text(s) + async def inline_respond_with_sticker(update: Update, context: ContextTypes.DEFAULT_TYPE): query = update.inline_query.query @@ -68,6 +83,8 @@ def main(): picture_handler = MessageHandler(filters.TEXT & ~filters.COMMAND, respond_with_picture) application.add_handler(start_handler) + + application.add_handler(MessageHandler(filters.PHOTO, image2string)) application.add_handler(picture_handler) application.add_handler(InlineQueryHandler(inline_respond_with_sticker)) diff --git a/render.py b/render.py index 4f2ea9b..f342cf5 100644 --- a/render.py +++ b/render.py @@ -65,6 +65,38 @@ def render_text_on_image(input_image_path, text_to_draw, output_image_path=None, return file +WIDTH = 22 +HEIGHT = 9 + +def image_to_string(image_file): + + img = Image.open(image_file) + + img = img.resize((WIDTH, HEIGHT), Image.Resampling.LANCZOS) + + + img = img.convert("1") + + # Get image dimensions + width, height = img.size + if width != 22 or height != 9: + raise ValueError("The image must be exactly 22x9 pixels.") + + # Create the string representation + pixel_string = "" + for y in range(height): + for x in range(width): + # Get the pixel value (0 for black, 255 for white) + pixel = img.getpixel((x, y)) + if pixel == 0: # Black pixel + pixel_string += "Ń" + else: # White pixel + pixel_string += "…" + pixel_string += " " # New line for each row + + return pixel_string + + if __name__ == "__main__": render_text_on_image("red_ebalo.png", " ".join(sys.argv[1:]), output_image_path="red_ebalo_text.png")