Python 如何使用Telethon从电报中获取图像字节
我正试图找到包含在我从以下电报频道下载的消息中的图像字节。但是,我不断收到错误消息,MessageMediaPhoto没有属性字节。以下是相关的代码片段:Python 如何使用Telethon从电报中获取图像字节,python,telegram,telethon,Python,Telegram,Telethon,我正试图找到包含在我从以下电报频道下载的消息中的图像字节。但是,我不断收到错误消息,MessageMediaPhoto没有属性字节。以下是相关的代码片段: if event.photo: id = event.message.to_id chat_username = client.get_entity(id) usr = chat_username.username image_base = e
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\u media
方法下载图像才能完成此操作。简单消息对象没有该信息 这最终对我有效:
photo_1 = Image.open(photo)
image_buf = BytesIO()
photo_1.save(image_buf, format="JPEG")
image = image_buf.getvalue()