【问题标题】:discord.py PILLOW image manipulation file not found未找到 discord.py PILLOW 图像处理文件
【发布时间】:2022-04-02 23:57:34
【问题描述】:
@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"))

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

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

【问题讨论】:

  • 您是否从脚本所在的同一目录运行脚本?
  • 这段代码是 Cogs/fun.py,wanted.png 是 Cogs/wanted.png

标签: python discord discord.py bots


【解决方案1】:

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

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 文件。

【讨论】:

  • 不客气。如果这对您有所帮助,您应该将其标记为其他人的答案,以查看此问题已解决。 :)
猜你喜欢
  • 2017-11-05
  • 1970-01-01
  • 2021-05-05
  • 2020-12-02
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多