反转图像错误

时间:2018-06-24 13:03:21

标签: python-3.x python-imaging-library

我正在尝试使用PIL反转从电报中下载的图像,然后将其发送回去:

import PIL, PIL.Image, PIL.ImageFont, PIL.ImageOps
from io import BytesIO

foto = msg["reply_to_message"]["photo"][0]["file_id"]
path = '/home/****/mysite/'+str(secrets.randbelow(10000))
bot.download_file(foto, path) #download the photo from telegram (it works)
img = PIL.Image.open(path).convert('RGBA')
img = PIL.ImageOps.invert(img)
final = BytesIO()
img.save(final, 'png')
final.seek(0)
bot.sendPhoto(chat_id, final) #send the photo in the telegram chat

但是异常处理程序向我发送此消息:

  

此图像模式不支持。

出什么问题了?

1 个答案:

答案 0 :(得分:0)

至少不能以这种方式反转具有透明度的图像。

最简单的选择是在convert语句中将RGBA更改为RGB。

如果需要处理透明图像,可以将图像分为r,g,b和alpha(a)通道,将r,g,b合并为一张图像,然后将其反转。然后,您重构所得通道r',g',b'和原始通道a的图像合并。