簡體   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