繁体   English   中英

找不到 discord.py PILLOW 图像处理文件

[英]discord.py PILLOW image manipulation file not found

@commands.command(name="wanted", aliases=["procurado"], description="Faz com que um usuário mencionado seja colocado em um cartaz de procurado")
    @commands.cooldown(1, 2, commands.BucketType.member)
    async def wanted(self, ctx, member:discord.Member = None):
      if member is None:
          member = ctx.author
      wanted = Image.open("wanted.png")
      asset = ctx.author.avatar_url_as(size = 128)
      data = BytesIO(await asset.read())
      pfp = Image.open(data)
      pfp = pfp.resize((144,144)) 
      wanted.paste(pfp, (48,106))
      wanted.save("wanteddata.jpg")
      await ctx.send(file = discord.File("wanteddata.jpg"))

want.png 位于代码的同一文件夹中,当我在控制台中使用该命令时出现此错误

Command raised an exception: FileNotFoundError: [Errno 2] No such file or directory: 'wanted.png'

假设根据您的评论,您的项目如下所示:

YourProject
├─ main.py
└─ Cogs
    ├─ fun.py
    └─ wanted.png

如果您从YourProject目录运行您的机器人,如下所示:

\Some\directories\YourProject> python main.py

该脚本试图访问YourProject/wanted.png而不是YourProject/Cogs/wanted.png 您可以通过 3 种方式修复它:

  1. 通过提供完整目录:
Image.open("/Some/directories/YourProject/Cogs/wanted.png")
  1. 通过指定文件夹名称(仅当您从YourProject目录运行脚本时才有效):
Image.open("Cogs/wanted.py")
  1. 通过使图像相对于您的fun.py文件:
import os
filepath = os.path.dirname(os.path.abspath(__file__))
Image.open(f"{filepath}/wanted.png")

我建议使用第三个选项,因为这样你就可以从任何你想要的地方运行你的 python 文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM