我的PIL(枕头)输出为NoneType



所以我有这个代码:

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给出了.web,但文档显示PIL可以处理.web文件。我用ByteIO加载了这两个imagesd,把它们放进PIL,粘贴在一起,我得到了这个错误:

Ignoring exception in command profile:
Traceback (most recent call last):
File "C:UsersNikanaconda3envsbeatsbotlibsite-packagesPILImageFile.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:UsersNikanaconda3envsbeatsbotlibsite-packagesdiscordextcommandscore.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:UsersNikanaconda3envsbeatsbotlibsite-packagesPILImage.py", line 1483, in paste
im.load()
File "C:UsersNikanaconda3envsbeatsbotlibsite-packagesPILImageFile.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:UsersNikanaconda3envsbeatsbotlibsite-packagesdiscordextcommandsbot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:UsersNikanaconda3envsbeatsbotlibsite-packagesdiscordextcommandscore.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:UsersNikanaconda3envsbeatsbotlibsite-packagesdiscordextcommandscore.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'

所以它一定是NoneType,但我不知道为什么。我很确定我把所有东西都装对了,但显然不是。

我很难重现这一点,但这部分看起来很奇怪。

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()

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

最新更新