【发布时间】:2018-12-24 23:49:16
【问题描述】:
对于我的电报机器人 (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
提前感谢 :) 圣诞快乐
【问题讨论】:
-
考虑使用tempfile
标签: python python-3.x type-conversion python-imaging-library python-telegram-bot