gpt4 book ai didi

python-3.x - 反转图像错误

转载 作者:行者123 更新时间:2023-12-03 16:50:25 25 4
gpt4 key购买 nike

我正在尝试使用 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

但是异常处理程序向我发送了这条消息:

Not supported for this image mode.



有什么问题?

最佳答案

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

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

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

关于python-3.x - 反转图像错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51010208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com