python Discord BOT 中的 Pillow 库无法识别 .paste 和 .save 函数
Pillow library in python Discord BOT doesn't recognise the .paste and .save functions
我正在搜索使用我的 discord 机器人和 pillow python 库制作自定义欢迎图像。
我不明白为什么这段代码不起作用。我要疯了...
(如果对变量名称有些混淆,请不要担心,因为我之前必须翻译这段代码)
import discord
import datetime
import PIL
from PIL import Image, ImageFont, ImageDraw
from discord import client, message
from discord.ext import commands
from discord.utils import get
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix="!", intents=intents)
client.remove_command('help')
@client.event
async def on_member_join(member):
await member.avatar_url.save('test.png')
pfp_unresized = Image.open('test.png')
pfp = pfp_unresized.resize((200, 200))
graph = Image.open("graph.png")
black_img = Image.open("Black_img.png")
bannerpfp = black_img.paste(pfp, (400, 30))
bannernotxt = bannerpfp.paste(graph, (0, 0))
await bannernotxt.save('bannernotxt.png')
这是回溯:
Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:\users\yuppi\appdata\local\programs\python\python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\yuppi\Documents\Nico\Codici\PYTHON\Discord\DiscordBOT\DiscordBOT.py", line 33, in on_member_join
bannernotxt = bannerpfp.paste(graph, (0, 0))
AttributeError: 'NoneType' object has no attribute 'paste'
希望你能帮助我。
正如我在评论中指出的那样,Image.paste
操作 就地 ,即它修改 现有 变量而不是 return正在修改它的版本。此类函数的 return 值始终是 None
,因为没有任何内容被 returned。
对于您的具体情况,这意味着调用 black_img.paste(pfp, (400, 30))
将直接修改 black_img
。如果您检查此调用之前和之后的图像,您会发现它们是不同的。因此,将您的代码修改为以下内容应该会给您带来期望的结果:
@client.event
async def on_member_join(member):
await member.avatar_url.save('test.png')
pfp_unresized = Image.open('test.png')
pfp = pfp_unresized.resize((200, 200))
graph = Image.open("graph.png")
black_img = Image.open("Black_img.png")
black_img.paste(pfp, (400, 30))
black_img.paste(graph, (0, 0))
await black_img.save('bannernotxt.png')
我正在搜索使用我的 discord 机器人和 pillow python 库制作自定义欢迎图像。 我不明白为什么这段代码不起作用。我要疯了...
(如果对变量名称有些混淆,请不要担心,因为我之前必须翻译这段代码)
import discord
import datetime
import PIL
from PIL import Image, ImageFont, ImageDraw
from discord import client, message
from discord.ext import commands
from discord.utils import get
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix="!", intents=intents)
client.remove_command('help')
@client.event
async def on_member_join(member):
await member.avatar_url.save('test.png')
pfp_unresized = Image.open('test.png')
pfp = pfp_unresized.resize((200, 200))
graph = Image.open("graph.png")
black_img = Image.open("Black_img.png")
bannerpfp = black_img.paste(pfp, (400, 30))
bannernotxt = bannerpfp.paste(graph, (0, 0))
await bannernotxt.save('bannernotxt.png')
这是回溯:
Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:\users\yuppi\appdata\local\programs\python\python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\yuppi\Documents\Nico\Codici\PYTHON\Discord\DiscordBOT\DiscordBOT.py", line 33, in on_member_join
bannernotxt = bannerpfp.paste(graph, (0, 0))
AttributeError: 'NoneType' object has no attribute 'paste'
希望你能帮助我。
正如我在评论中指出的那样,Image.paste
操作 就地 ,即它修改 现有 变量而不是 return正在修改它的版本。此类函数的 return 值始终是 None
,因为没有任何内容被 returned。
对于您的具体情况,这意味着调用 black_img.paste(pfp, (400, 30))
将直接修改 black_img
。如果您检查此调用之前和之后的图像,您会发现它们是不同的。因此,将您的代码修改为以下内容应该会给您带来期望的结果:
@client.event
async def on_member_join(member):
await member.avatar_url.save('test.png')
pfp_unresized = Image.open('test.png')
pfp = pfp_unresized.resize((200, 200))
graph = Image.open("graph.png")
black_img = Image.open("Black_img.png")
black_img.paste(pfp, (400, 30))
black_img.paste(graph, (0, 0))
await black_img.save('bannernotxt.png')