add image2string

This commit is contained in:
owl 2024-12-09 02:32:46 +07:00
parent 4f3dc460fc
commit 2a98b55b8d
3 changed files with 53 additions and 2 deletions

View File

@ -1 +1,3 @@
.env .env
red_eb/
venv/

19
bot.py
View File

@ -2,7 +2,8 @@
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters, InlineQueryHandler from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters, InlineQueryHandler
import logging 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 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) file = render_text_on_image("red_ebalo.png", update.message.text)
await context.bot.send_sticker(chat_id=update.effective_chat.id, sticker=file) 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): async def inline_respond_with_sticker(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.inline_query.query query = update.inline_query.query
@ -68,6 +83,8 @@ def main():
picture_handler = MessageHandler(filters.TEXT & ~filters.COMMAND, respond_with_picture) picture_handler = MessageHandler(filters.TEXT & ~filters.COMMAND, respond_with_picture)
application.add_handler(start_handler) application.add_handler(start_handler)
application.add_handler(MessageHandler(filters.PHOTO, image2string))
application.add_handler(picture_handler) application.add_handler(picture_handler)
application.add_handler(InlineQueryHandler(inline_respond_with_sticker)) application.add_handler(InlineQueryHandler(inline_respond_with_sticker))

View File

@ -65,6 +65,38 @@ def render_text_on_image(input_image_path, text_to_draw, output_image_path=None,
return file 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__": if __name__ == "__main__":
render_text_on_image("red_ebalo.png", " ".join(sys.argv[1:]), output_image_path="red_ebalo_text.png") render_text_on_image("red_ebalo.png", " ".join(sys.argv[1:]), output_image_path="red_ebalo_text.png")