Python Forum

Full Version: aoigram, pil Help with photo processing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
(python and libraries are all last) When sending a photo, it gives an error in the bot, but there is nothing in the terminal


from datetime import datetime
import io

from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Text
from aiogram.dispatcher.filters.state import State, StatesGroup
from asyncio import sleep
from PIL import Image

from keyboards import inline, default
from misc import dp, bot, only_numerics
import fakedraw

class Settings(StatesGroup):
    zakaz = State()
    add_product_photo = State()
users_data = {} 
last_bot_photo = None

@dp.message_handler(lambda message: message.text == "Главное меню", content_types=types.ContentTypes.TEXT, state="*")
async def main(message: types.Message, state: FSMContext):
    await message.answer("Главное меню", reply_markup=default.Start())
    await state.finish()


@dp.message_handler(lambda message: message.text == "Заказ оплачен", content_types=types.ContentTypes.TEXT, state="*")
async def zakaz(message: types.Message, state: FSMContext):
    await message.answer("Выберите действие с помощью клавиатуры", reply_markup=default.Zakaz())
    await Settings.zakaz.set()

@dp.message_handler(lambda message: message.text == "Ввести данные", content_types=types.ContentTypes.TEXT, state=Settings.zakaz)
async def zakaz(message: types.Message, state: FSMContext):
    await message.answer_photo(fakedraw.fake_zakaz(message), caption="""
Введи необходимые данные:

1️⃣ - Время
2️⃣ - Первая буква именни аккаунта (Пример: Н)
3️⃣ - Имя аккаунта (Пример: Николай)
4️⃣ - Название товара (Пример: Тачка на прокачку)

Пример:
14:37
Н
Николай
Тачка на прокачку)
""")
    await Settings.zakaz.set()


@dp.message_handler(content_types=types.ContentTypes.TEXT, state=Settings.zakaz)
async def zakaz(message: types.Message, state: FSMContext):
    await message.answer_photo(fakedraw.fake_zakaz(message), reply_markup=inline.AddPhoto())
    await Settings.next()

@dp.message_handler(content_types=types.ContentTypes.PHOTO, state=Settings.add_product_photo)
async def add_product_photo(message: types.Message, state: FSMContext):
    user_data = await state.get_data()
    user_id = user_data.get("user_id")
    order_data = user_data.get("order_data")
    global last_bot_photo
    if last_bot_photo:
        last_bot_photo_data = await bot.get_file(last_bot_photo)
        last_bot_photo_bytes = await bot.download_file(last_bot_photo_data.file_path)
        last_bot_photo_stream = io.BytesIO(last_bot_photo_bytes)
        last_bot_photo_stream.name = 'last_bot_photo.jpg'
    else:
        await message.answer("Произошла ошибка...")
        await state.finish()
        return
    product_photo_data = await bot.get_file(message.photo[-1].file_id)
    product_photo_bytes = await bot.download_file(product_photo_data.file_path)
    product_photo_stream = io.BytesIO(product_photo_bytes)
    product_photo_stream.name = 'product_photo.jpg'
    combined_photo_stream = io.BytesIO()
    with Image.open(last_bot_photo_stream) as img1, Image.open(product_photo_stream) as img2:
        img1.paste(img2, (0, 0), img2)
        img1.save(combined_photo_stream, format='JPEG')
    await message.answer_photo(combined_photo_stream.getvalue(), caption="Обработанное фото с текстом и добавленным товаром", reply_markup=inline.AddPhoto())
    users_data[user_id]["product_photo_id"] = message.photo[-1].file_id
    await Settings.add_product_photo.set()

@dp.callback_query_handler(text='add_photo', state=Settings.add_product_photo)
async def ask_for_product_photo(call: types.CallbackQuery, state: FSMContext):
    await call.message.answer("Отправьте фото товара")
    await Settings.add_product_photo.set()

@dp.message_handler(lambda message: message.text == "📈 Информация", content_types=types.ContentTypes.TEXT, state="*")
async def info(message: types.Message, state: FSMContext):
    await message.answer(f"Тайная разработка компании - НессаАйтиЧиллКомпани")[python]
[/python]