我该如何编码一个不和谐的机器人,使其能够使用python PIL发布修改过的gif和/或png个人资料图片? [英] How do I code a discord bot so it becomes able to post modified gif and/or png profile picture using python PIL?

查看:18
本文介绍了我该如何编码一个不和谐的机器人,使其能够使用python PIL发布修改过的gif和/或png个人资料图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码适用于个人资料图片中具有 .png 格式的用户,但是,当涉及具有 .gif 动画个人资料图片的用户时,该代码不行.它从e OSError中给出此错误 OSError(f无法将模式{mode}写入为PNG"):无法将模式PA写入为PNG

我试图将所有 .png 都更改为 .gif ,但是仍然遇到问题.

ValueError:图片的模式错误

这是上述代码, 仅适用于 ,格式为 .png .

  class化身(commands.Cog):def __init __(自己,客户):self.client =客户@ commands.Cog.listener()异步定义on_member_join(self,member):公会= self.client.get_guild(GUILD_ID)general_channel = guild.get_channel(CHANNEL_ID)url = requests.get(member.avatar_url)头像= Image.open(BytesIO(url.content))头像= avatar.resize((285,285));bigsize =(avatar.size [0] * 3,avatar.size [1] * 3)遮罩= Image.new('L',bigsize,0)绘制= ImageDraw.Draw(mask)draw.ellipse(((0,0)+ bigsize,fill = 255)mask = mask.resize(avatar.size,Image.ANTIALIAS)avatar.putalpha(掩码)输出= ImageOps.fit(头像,mask.size,居中=(1420,298))output.putalpha(掩码)output.save('avatar.png')img = Image.open('welcomealpha.png')img.paste(头像,(1408,265),头像)img.save('wel.png')文件= discord.File('wel.png')频道= self.client.get_channel(CHANNEL_ID)等待channel.send(file = file)公会= self.client.get_guild(GUILD_ID)频道= guild.get_channel(CHANNEL_ID) 

可能是因为该漫游器不知道如何区分 .gif & .png ?如果真是这样,对于每个新用户来说,bot 识别哪种个人资料图片格式,以便根据其格式相应地操作图片/gif,最有效的方法是什么?

解决方案

错误消息在这里非常清楚:您原始的 Image 对象具有

导出的 PNG 如下;它似乎是 GIF 的第一帧:

  ----------------------------------------系统信息----------------------------------------平台:Windows-10-10.0.16299-SP0的Python:3.9.1枕头:8.1.0---------------------------------------- 

This code works with users that have .png format in their profile pictures, however, when it comes to users that have .gif animated profile pictures, the code does not work. It gives this error OSError(f"cannot write mode {mode} as PNG") from e OSError: cannot write mode PA as PNG

I attempted to change all .png to .gif but I still had trouble.

ValueError: image has wrong mode

This is the aforementioned code that only works with .png format.

class avatar(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_member_join(self, member):
        guild = self.client.get_guild(GUILD_ID)
        general_channel = guild.get_channel(CHANNEL_ID)


        url = requests.get(member.avatar_url)
        avatar = Image.open(BytesIO(url.content))
        avatar = avatar.resize((285,285));
        bigsize = (avatar.size[0] * 3,  avatar.size[1] * 3)
        mask = Image.new('L', bigsize, 0)
        draw = ImageDraw.Draw(mask)
        draw.ellipse((0, 0) + bigsize, fill=255)
        mask = mask.resize(avatar.size, Image.ANTIALIAS)
        avatar.putalpha(mask)

        output = ImageOps.fit(avatar, mask.size, centering=(1420, 298))
        output.putalpha(mask)
        output.save('avatar.png')

        img = Image.open('welcomealpha.png')
        img.paste(avatar,(1408,265), avatar)
        img.save('wel.png')

        file = discord.File('wel.png')
        channel = self.client.get_channel(CHANNEL_ID)
        await channel.send(file=file)
        guild = self.client.get_guild(GUILD_ID)
        channel = guild.get_channel(CHANNEL_ID)

Could it be that the bot doesn't know how to discern between .gif & .png ? If that's the case, what would be the most efficient way for the bot to recognize which profile picture format each new user has in order to manipulate image/gif accordingly to its format?

解决方案

The error message is quite clear here: Your original Image object has mode P, i.e. it's a palettised image. When adding an alpha channel as you did, you get mode PA. As Pillow tells you, saving Image objects with mode PA as PNG is not supported. Since you only want to save to some static PNG without any animation, I assume it's save to convert the Image object to mode RGB right in the beginning, such that you get a RGBA mode Image object in the end, which can be saved as PNG without any problems.

I took the following excerpt from your code and added the conversion to mode RGB:

from PIL import Image, ImageDraw, ImageOps

avatar = Image.open('homer.gif').convert('RGB')
avatar = avatar.resize((285, 285))
bigsize = (avatar.size[0] * 3,  avatar.size[1] * 3)
mask = Image.new('L', bigsize, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + bigsize, fill=255)
mask = mask.resize(avatar.size, Image.ANTIALIAS)
avatar.putalpha(mask)

output = ImageOps.fit(avatar, mask.size, centering=(1420, 298))
output.putalpha(mask)
output.save('avatar.png')

The GIF input is Homer; the corresponding Image object has mode P:

The exported PNG is the following; it seems to be the first frame of the GIF:

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
Pillow:        8.1.0
----------------------------------------

这篇关于我该如何编码一个不和谐的机器人,使其能够使用python PIL发布修改过的gif和/或png个人资料图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆