我的PIL(枕头)以非类型输出

2024-04-24 06:53:40 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有这个代码:

avurl = str(ctx.message.author.avatar_url) avurl = avurl.replace("?size=1024", "") async with aiohttp.ClientSession() as session: async with session.get(avurl) as second_image: image_bytes = await second_image.read() with Image.open(BytesIO(image_bytes)) as first_image: output_buffer = BytesIO() first_image.save(output_buffer, "png") output_buffer.seek(0) async with aiohttp.ClientSession() as session: async with session.get("https://i.imgur.com/dNS0WJO.png") as second_image: image_bytes = await second_image.read() with Image.open(BytesIO(image_bytes)) as second_image: output_buffer = BytesIO() first_image.save(output_buffer, "png") output_buffer.seek(0) first_image.paste(second_image, (0, 0)) buf = io.BytesIO() first_image.save(buf, "png") file = discord.File(fp=first_image, filename="pfp.png") if arg1 is None: embed = discord.Embed(title=" ", color=0xD1661E) embed.set_author(name="Your Profile", icon_url=url1) embed.set_thumbnail(url="attachment://pfp.png") await ctx.send(embed=embed, file=file)

我试着拍两张图片,一张是用户不和谐的个人资料图片,然后用透明的png进行分层。阿凡达url提供了一个.webp文件,但文档显示PIL可以处理.webp文件。 我用BytesIO加载了两个imagesd,将它们放到PIL中,然后将它们粘贴在一起,我得到了这个错误:

Ignoring exception in command profile: Traceback (most recent call last): File "C:\Users\Nik\anaconda3\envs\beatsbot\lib\site-packages\PIL\ImageFile.py", line 166, in load seek = self.load_seek AttributeError: 'PngImageFile' object has no attribute 'load_seek' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Nik\anaconda3\envs\beatsbot\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped ret = await coro(*args, **kwargs) File "C:/Users/Nik/PycharmProjects/beatsbot/beats.py", line 2711, in profile first_image.paste(second_image, (0, 0)) File "C:\Users\Nik\anaconda3\envs\beatsbot\lib\site-packages\PIL\Image.py", line 1483, in paste im.load() File "C:\Users\Nik\anaconda3\envs\beatsbot\lib\site-packages\PIL\ImageFile.py", line 169, in load seek = self.fp.seek AttributeError: 'NoneType' object has no attribute 'seek' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\Nik\anaconda3\envs\beatsbot\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke await ctx.command.invoke(ctx) File "C:\Users\Nik\anaconda3\envs\beatsbot\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke await injected(*ctx.args, **ctx.kwargs) File "C:\Users\Nik\anaconda3\envs\beatsbot\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'seek'

所以它一定是非类型的,但我不知道为什么。我很确定我把所有东西都装好了,但显然不是


Tags: inpyimagepnglineseekusersfile
1条回答
网友
1楼 · 发布于 2024-04-24 06:53:40

我很难复制这个,但是这部分看起来很奇怪

with Image.open(BytesIO(image_bytes)) as second_image:
     output_buffer = BytesIO()
     first_image.save(output_buffer, "png")
     output_buffer.seek(0)

这是您第二次调用first_image.save(),但我希望此块调用second_image.save()

抱歉,如果这只是对代码的错误解释

相关问题 更多 >