Telegram 将 png 图片发送到 Telegram 将其转换为 jpg

Telegram sending png picture to Telegram converts it to jpg

为什么我把png图片发给telegram bot,然后下载下来,就变成了jpg?如何避免?

MY_USER_ID = 012345
MY_TOKEN = "12345"
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)

from telegram.ext import Updater, MessageHandler, Filters, CommandHandler

updater = Updater(token=MY_TOKEN, use_context=True)

updater.start_polling()

dispatcher = updater.dispatcher


def create_sticker_set(update, context):

    update_dict = update.to_dict()

    sticker_file_id = update_dict["message"]["photo"][-1]["file_id"]

    print("sticker_file_id=", sticker_file_id)

    file = context.bot.get_file(update_dict["message"]["photo"][-1]["file_id"])
    filename = file.download() # it is jpg, and it must be png

    context.bot.add_sticker_to_set(MY_USER_ID, "lala_by_ibodi_bot", open(filename, "rb"), "")


msghandler = MessageHandler(Filters.photo, create_sticker_set)
dispatcher.add_handler(msghandler)

此代码创建一个处理程序,用于接收图片并将其添加到贴纸集。我想保留图片的扩展,以便图片的透明部分在贴纸集中保持透明。并将其转换为 jpg 使透明的地方变白。

我找到了解决办法。当我发送文件时,我应该选择 "Send as a file" 而不是 "Send as a photo",它不会将 png 转换为 jpg。

上面的代码必须有一些变化:

# we put 
msghandler = MessageHandler(Filters.document, create_sticker_set)
# instead of instead of
msghandler = MessageHandler(Filters.photo, create_sticker_set)

# we put 
file = context.bot.get_file(update_dict["message"]["document"]["file_id"])
# instead of 
file = context.bot.get_file(update_dict["message"]["photo"][-1]["file_id"])