1

I am attempting to make a simple pygame game, and I need to load in some images. When I run my code, or:

bg_image = pygame.image.load('assets/Fondotiff.tif(1).png').convert_alpha()

It gives me an error which says:

    Traceback (most recent call last):
        File "C:\Users\victo\Desktop\Assets - Pulga\mainpulga.py", line 12, in <module>
    bg_image = pygame.image.load('assets/Fondotiff.tif(1).png').convert_alpha()
FileNotFoundError: No file 'assets/Fondotiff.tif(1).png' found in working directory 'C:\Users\victo\Desktop\Assets - Pulga'.
    
    ***Repl Closed***

I have double checked if the image is inside the folder on my desktop and it is there, but for some reason it doesn't find it.

How can I fix this?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

2

The asset file path has to be relative to the current working directory. The working directory is possibly different from the directory of the python file. It is not enough to put the files in the same directory or sub directory. You also need to set the working directory.

e.g.:

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

Another way you can do it is simply by also adding the file name of the file the script is inside of e.g.

image = pygame.image.load('-file with script inside-/-file with image inside-/-name of image-

Also make sure that the format of the image is compatible with pygame.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34535734) – dpapadopoulos Jun 16 '23 at 10:23