pygame.error: Couldn't open image

拥有回忆 提交于 2019-11-27 06:29:07

问题


I am learning python using this website: http://programarcadegames.com/index.php?lang=en&chapter=bitmapped_graphics_and_sound

I am trying to load an image like so:

background_image = pygame.image.load("background.jpg").convert()

I have found several other asking the same question, and all the answers are telling the poster to make sure the image is in the same folder as the .py file. Well, it is. I double checked.

I also tried putting it into a subfolder called 'images' and using this.

background_image = pygame.image.load(os.path.join("images","background.jpg")).convert()

In both cases I get the following error message.

pygame.error: Couldn't open saturn_family1.jpg
or    
pygame.error: Couldn't open images\saturn_family1.jpg

When using the aboslute path, it works. But shouldn't I be able to use a relative path like this?

Also, this happens when using the "script" package to execute in Atom. But when executing the actual python file, it works.

I am very frustrated, this doesn't seem to make any sense! What's going on?

EDIT: Full error message

C:\Users\Manu\Dropbox\Python\ProgramArcadeGames
Traceback (most recent call last):
  File "C:\Users\Manu\Dropbox\Python\ProgramArcadeGames\Ch11\11_graphicsAndSound.py", line 26, in <module>
    background_image = pygame.image.load(os.path.join("images","saturn_family1.jpg")).convert()
pygame.error: Couldn't open images\saturn_family1.jpg
[Finished in 0.815s]

Added images of my directory:

UPDATE: I ran the exact same code in sublime and it worked fine. I am assuming this is a problem with Atom, so I will just use Sublime from now on. Thanks to everyone who tried to help!


回答1:


Putting the file in the same directory as your python program will work provided that is the current working directory. Since we know this is not the case, either you are somehow changing the working directory while the script is executing, or you are starting in some other directory. You could do this on the command line with

python ../myscript.py

but I don't suppose you're doing this. You say something about running the script under Atom, which may explain it. I don't have any experience with this, so I can't say offhand. Please put

print os.getcwd()

as the very first line in your program. Then you'll know where you're starting and whether the current directory is changing. If it's not changing, try running the script under Atom and from the command line to see if you get different results. We won't be able to solve the problem until we know exactly what is happening.




回答2:


Same error happened when I was coding with VS. I fixed that by changing the path of the image (use an absolute path here) .

Change pygame.image.load('/images/xxx') to

pygame.image.load('/Users/xxx/..../images/xxxx')

I am using the mac but it should be the same on other OS.




回答3:


Just use the absolute path for the picture. For example:

self.image = pygame.image.load('C:/Users/denis/Documents/python_practice/git_practice/Python_lessons/alien_invasion/images/ship.bmp')


来源:https://stackoverflow.com/questions/32684198/pygame-error-couldnt-open-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!