K
K
KiraHerschel2022-02-26 02:58:33
Python
KiraHerschel, 2022-02-26 02:58:33

How to overlay one image on another in a telegram bot?

It is necessary that the bot processes the photo sent by the user and overlays a png file from the disk on it

def on_message(update, context):
        chat = update.effective_chat
        file = bot.get_file(update.message.photo[-1].file_id)
        
        vesna = Image.open('C://Users//79516//Documents//vesna.png')
        photo = Image.open(file.file_path)
        #photo.paste(vesna)
        #photo.save('C:Users//79516//Documents//photovesna.png', 'rb')
        bot.send_photo(chat_id=chat.id, photo= open('C://Users//79516//Documents//photovesna.png', 'rb'))  
        vesna.close()
        photo.close()
        context.bot.send_message(chat_id=chat.id, text="hi")

                
updater = Updater(token, use_context=True)

dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", on_start))
dispatcher.add_handler(MessageHandler(Filters.photo, on_message))

updater.start_polling()
updater.idle()

Now it gives an error:

FileNotFoundError: [Errno 2] No such file or directory: 'photos/file_1.jpg'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-02-26
@KiraHerschel

I suspect that file is a file object and not a file on disk. So file_path doesn't match any path on your machine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question