For my telegram bot (python-telegram-bot) i generated a PIL.Image.Image and i want to send it directly to a user.
What works is to send an image as bufferedReader from a file, but i don't want to safe the image. I don't need it again afterwards and i might generate a lot of different images at the same time, so saving is kind of messy.
bot.send_photo(chat_id=update.message.chat_id,
photo=open(img_dir, 'rb'),
caption='test',
parse_mode=ParseMode.MARKDOWN)
Because i generated it myself, i cant use an URL or file_id. I thought it might be possible to convert the image to a bufferedReader, but i only managed to get a bytes object from it, which didn't work.
The image is generated like:
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
Thanks in advance :) merry x-mas
Have a lock at this code snippet from the packages github wiki
Post an image from memory
In this example, image is a PIL (or Pillow) Image object, but it works the same with all media types.
from io import BytesIO
bio = BytesIO()
bio.name = 'image.jpeg'
image.save(bio, 'JPEG')
bio.seek(0)
bot.send_photo(chat_id, photo=bio)