'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"))

wanted.png are in the same folder of the code, when i use the command in console get this error

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


Solution 1:[1]

Assuming from your comment your project looks like this:

YourProject
?? main.py
?? Cogs
    ?? fun.py
    ?? wanted.png

If you are running your bot from the YourProject directory like so:

\Some\directories\YourProject> python main.py

The script is trying to access YourProject/wanted.png not YourProject/Cogs/wanted.png. You can fix it in 3 ways:

  1. By providing full directory:
Image.open("/Some/directories/YourProject/Cogs/wanted.png")
  1. By specifing folder name (It will only work if you run your script from YourProject directory):
Image.open("Cogs/wanted.py")
  1. By making image relative to your fun.py file:
import os
filepath = os.path.dirname(os.path.abspath(__file__))
Image.open(f"{filepath}/wanted.png")

I recommend using the 3rd option because then you can run your python file from wherever you want.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 loloToster