2

I have a Pygame project in a folder called "Project". In it is my code and another folder named "Images". I want to load an image in the "Images" folder, but every time I try to do so, it comes with up an error, saying:

pygame.error: Couldn't open Images/Frame1.png

I've tried using:

image = pygame.image.load("Frame1.png"),

image = pygame.image.load("Images/Frame1.png") , image = pygame.image.load("Images/Frame1.png").convert() and image = pygame.image.load("Images/Frame1.png").convert_alpha() .

Bare in mind that I've only imported the Pygame module.

All the codes above come up with the same error. I'm using Python v3.7.3 for Mac OSX. Any help would be greatly appreciated.

sombat92
  • 109
  • 8
  • share your folder structure please – Vitor Falcão Aug 21 '19 at 17:14
  • What is the working directory (`os.getcwd()`) and what is the directory of the python file when you run the code (`os.path.dirname(os.path.abspath(__file__))`)? The path has to be relative to the working directory and the working directory may be different to the directory of the file (or project). – Rabbid76 Aug 21 '19 at 17:14
  • The image's directory is `~/Python_Projects/Project/Images/Frame1.png`. My code's directory is `~/Python_Projects/Project/FirstProject.py` – sombat92 Aug 21 '19 at 17:16
  • Where are you executing from? Try running python and your script from "Project" folder. – Catsunami Aug 21 '19 at 17:18
  • @sombat92 try `import os` `print(os.getcwd())`. The path has to be relative to this directory. Or put `os.chdir( os.path.dirname(os.path.abspath(__file__)) )` at the begin of the code. – Rabbid76 Aug 21 '19 at 17:18
  • @Rabbid76 the output is above – sombat92 Aug 21 '19 at 17:19
  • @sombat92 No, it isn't. This is not the working directory. – Rabbid76 Aug 21 '19 at 17:20
  • @Rabbid76 The output of `print(os.getcwd())` is "/Users/mustafaabdillahi06/Python_Projects/Project" --- the output of `print(os.chdir( os.path.dirname(os.path.abspath(__file__)) ))` is "None" – sombat92 Aug 21 '19 at 17:28

2 Answers2

2

Try importing the os library and using image = pygame.image.load(os.path.join(os.path.dirname(__file__), 'Images','Frame1.png')).convert_alpha(). This worked for me when I did my own pygame project.

the_redcar
  • 129
  • 1
  • 1
  • 10
1

The image filepath has to be relative to the current working directory. The working directory is possibly different to the directory of the python file.

The difference can be investigated by:

import os

currentWorkDir = os.getcwd()
print(currentWorkDir)

sourceFileDir = os.path.dirname(os.path.abspath(__file__))
print(sourceFileDir)

See also Import-related module attributes.

The current working directory can be changed by the application, to be the same as the python source file directory:

import os

sourceFileDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(sourceFileDir)

Instead of changing the working directory, you can also use an absolut path when you load the image file. Concatenate the directory of the file and the relative image filepath:

sourceFileDir = os.path.dirname(os.path.abspath(__file__))
image = pygame.image.load(os.path.join(sourceFileDir, "Images", "Frame1.png"))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174