我的 PIL(枕头)输出为 NoneType
Posted
技术标签:
【中文标题】我的 PIL(枕头)输出为 NoneType【英文标题】:My PIL (Pillow) is outputting as NoneType 【发布时间】:2020-11-29 15:11:43 【问题描述】:所以我有这个代码:
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)
我正在尝试拍摄两张图片,一张是用户的 Discord 个人资料图片,然后在其上叠加一个具有透明度的 png。头像 url 给出了 .webp 但文档说 PIL 可以处理 .webp 文件。 我用 BytesIO 加载了两个图像,将它们放入 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'
所以它必须是 NoneType,但我不知道为什么。我很确定我正确加载了所有内容,但显然不是。
【问题讨论】:
【参考方案1】:我很难重现这一点,但是这部分看起来很奇怪。
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()
抱歉,这只是对代码的错误解释。
【讨论】:
以上是关于我的 PIL(枕头)输出为 NoneType的主要内容,如果未能解决你的问题,请参考以下文章