如何通过电报机器人发送 PIL 图像而不将其保存到文件中
How to send an PIL Image via telegram bot without saving it to a file
对于我的电报机器人 (python-telegram-bot),我生成了一个 PIL.Image.Image,我想将它直接发送给用户。
有效的方法是从文件中将图像作为 bufferedReader 发送,但我不想保护图像。之后我就不需要它了,我可能会同时生成很多不同的图像,所以保存起来有点乱。
bot.send_photo(chat_id=update.message.chat_id,
photo=open(img_dir, 'rb'),
caption='test',
parse_mode=ParseMode.MARKDOWN)
因为是我自己生成的,所以我不能使用 URL 或 file_id。我认为可以将图像转换为 bufferedReader,但我只能设法从中获取字节对象,但没有用。
图像生成如下:
images = [Image.open(i) for i in dir_list]
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGBA', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset, 0))
x_offset += im.size[0]
return new_im # returns a PIL.Image.Image
提前致谢 :) 圣诞快乐
从包 github wiki
锁定此 code snippet
Post 记忆中的图像
在此示例中,图像是 PIL(或 Pillow)图像对象,但它对所有媒体类型的作用相同。
from io import BytesIO
bio = BytesIO()
bio.name = 'image.jpeg'
image.save(bio, 'JPEG')
bio.seek(0)
bot.send_photo(chat_id, photo=bio)
不知道您是否有兴趣发送 GIF 动画,但此代码片段应该对那些愿意的人有所帮助:
from telegram import Update, ForceReply
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
def echo_gif(update: Update, context: CallbackContext) -> None:
""" Echo to /gif command """
context.bot.sendAnimation(chat_id=update.message.chat_id,
animation=open("URuEc5hnbNIGs.gif", 'rb').read(), ## some local file name
caption='That is your gif!',
)
return
def main() -> None:
"""Start the bot."""
# Create the Updater and pass it your bot's token.
updater = Updater(TOKEN)
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# on different commands - answer in Telegram
dispatcher.add_handler(CommandHandler("gif", echo_gif))
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
对于我的电报机器人 (python-telegram-bot),我生成了一个 PIL.Image.Image,我想将它直接发送给用户。
有效的方法是从文件中将图像作为 bufferedReader 发送,但我不想保护图像。之后我就不需要它了,我可能会同时生成很多不同的图像,所以保存起来有点乱。
bot.send_photo(chat_id=update.message.chat_id,
photo=open(img_dir, 'rb'),
caption='test',
parse_mode=ParseMode.MARKDOWN)
因为是我自己生成的,所以我不能使用 URL 或 file_id。我认为可以将图像转换为 bufferedReader,但我只能设法从中获取字节对象,但没有用。
图像生成如下:
images = [Image.open(i) for i in dir_list]
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGBA', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset, 0))
x_offset += im.size[0]
return new_im # returns a PIL.Image.Image
提前致谢 :) 圣诞快乐
从包 github wiki
锁定此 code snippetPost 记忆中的图像
在此示例中,图像是 PIL(或 Pillow)图像对象,但它对所有媒体类型的作用相同。
from io import BytesIO
bio = BytesIO()
bio.name = 'image.jpeg'
image.save(bio, 'JPEG')
bio.seek(0)
bot.send_photo(chat_id, photo=bio)
不知道您是否有兴趣发送 GIF 动画,但此代码片段应该对那些愿意的人有所帮助:
from telegram import Update, ForceReply
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
def echo_gif(update: Update, context: CallbackContext) -> None:
""" Echo to /gif command """
context.bot.sendAnimation(chat_id=update.message.chat_id,
animation=open("URuEc5hnbNIGs.gif", 'rb').read(), ## some local file name
caption='That is your gif!',
)
return
def main() -> None:
"""Start the bot."""
# Create the Updater and pass it your bot's token.
updater = Updater(TOKEN)
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# on different commands - answer in Telegram
dispatcher.add_handler(CommandHandler("gif", echo_gif))
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()