viewing paste Unknown #58528 | Python

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
from PIL import Image, ImageDraw, ImageFont
 
TOKEN = "7830309255:AAHzpZfL62dW8haX41-x5lbUac6oEU1rke4"
 
 
def generator_mem(img_file, top_text, bottom_text):
    image = Image.open(img_file)
    width, height = image.size
 
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("v_CCTimSale_v1.11.ttf", size=60)
 
    # Расчет позиции для верхнего текста
    text_bbox = draw.textbbox((0, 0), top_text, font=font)
    text_width = text_bbox[2] - text_bbox[0]
    draw.text(((width - text_width) // 2, 10), top_text, font=font, fill="black")
 
    # Расчет позиции для нижнего текста
    text_bbox = draw.textbbox((0, 0), bottom_text, font=font)
    text_width = text_bbox[2] - text_bbox[0]
    text_height = text_bbox[3] - text_bbox[1]
    draw.text(((width - text_width) // 2, height - text_height - 50), bottom_text, font=font, fill="black")
 
    image.save("mem.jpg")
 
 
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text("Привет, генератор мемов запущен! Введите верхний текст:")
    context.user_data["state"] = "awaiting_top_text"
 
 
async def handle_text(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    user_text = update.message.text
    state = context.user_data.get("state")
 
    if state == "awaiting_top_text":
        context.user_data["top_text"] = user_text
        await update.message.reply_text("Теперь введите нижний текст: ")
        context.user_data["state"] = "awaiting_bottom_text"
 
    elif state == "awaiting_bottom_text":
        context.user_data["bottom_text"] = user_text
        await update.message.reply_text("Выбери картинку: 1:Бу испугался 2:Смешной кот 3:Черная рамка мем 4:Джокер")
        context.user_data["state"] = "awaiting_image_choice"
 
    elif state == "awaiting_image_choice":
        if user_text in ["1", "2", "3", "4"]:
            # Сохраняем оригинальную конструкцию выбора
            if user_text == "1":
                img_file = "cat.jpg"
            elif user_text == "2":
                img_file = "dog.jpg"
            elif user_text == "3":
                img_file = "rabbit.jpeg"
            elif user_text == "4":
                img_file = "winner.jpeg"
 
            top_text = context.user_data['top_text']
            bottom_text = context.user_data['bottom_text']
 
            generator_mem(img_file, top_text, bottom_text)
            await update.message.reply_photo(photo=open("mem.jpg", 'rb'))
            await update.message.reply_text("Мем готов! Используй /start чтобы сделать новый мем")
            context.user_data.clear()
 
    else:
        await update.message.reply_text("Пожалуйста, сначала введите /start")
 
 
def main():
    application = Application.builder().token(TOKEN).build()
    application.add_handler(CommandHandler("start", start))
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_text))
    application.run_polling()
 
 
if __name__ == "__main__":
    main()
Viewed 729 times, submitted by Guest.