繁体   English   中英

如何使用 Telethon 从电报消息中获取图像字节

[英]How to get image bytes from telegram message using Telethon

我正在尝试找到我正在从我关注的电报频道下载的消息中包含的图像字节。 但是,我不断收到 MessageMediaPhoto 没有属性字节的错误。 以下是相关的代码片段:

  if event.photo:
            id = event.message.to_id
            chat_username = client.get_entity(id)
            usr = chat_username.username
            image_base = event.message.media
            image_bytes = image_base.photo.bytes
            message = event.message.id
            url = ("https://t.me/" + str(usr) + "/" + str(message))
            print(url)
            print(image_bytes)

为此,您必须先使用download_media方法下载映像。 一个简单的Message对象没有该信息。

最终为我工作:

photo_1 = Image.open(photo)
image_buf = BytesIO()
photo_1.save(image_buf, format="JPEG")
image = image_buf.getvalue()

我们可以使用 Python 的BytesIO库中的io

from io import BytesIO

image_bytes = BytesIO()
event.message.download_media(image_bytes)

然后,我们在image_bytes中获取消息的图像字节。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM