Python 无法在 Atom 中使用 PyGame 打开图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32684198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:05:03  来源:igfitidea点击:

Cannot Open Images With PyGame in Atom

pythonwindowspygameatom-editor

提问by Manu

I am learning python using this website

我正在使用这个网站学习 python

I am trying to load an image like so:

我正在尝试像这样加载图像:

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

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

我发现其他几篇帖子都在问同样的问题,所有的答案都告诉发帖人确保图像与文件位于同一文件夹中.py。嗯,是的。我仔细检查了一遍。

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

我还尝试将它放入一个名为'images'并使用它的子文件夹中。

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 absolute path, it works. But shouldn't I be able to use a relative path like this?

使用绝对路径时,它有效。但是我不应该能够使用这样的相对路径吗?

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

此外,当使用script包在 Atom 中执行时会发生这种情况。但是当执行实际的 python 文件时,它可以工作。

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

我很沮丧,这似乎没有任何意义!这是怎么回事?

The full error message is:

完整的错误信息是:

C:\Users\Manu\Dropbox\Python\ProgramArcadeGames
Traceback (most recent call last):
  File "C:\Users\Manu\Dropbox\Python\ProgramArcadeGames\Ch11_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]

This is the .pydirectory: The <code>.py</code>directory

这是.py目录: <code>.py</code>目录

The <code>.py</code>directory

<code>.py</code>目录

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!

更新:我在 sublime 中运行了完全相同的代码,并且运行良好。我假设这是 Atom 的问题,所以从现在开始我将只使用 Sublime。感谢所有试图提供帮助的人!

采纳答案by saulspatz

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 程序相同的目录中。由于我们知道情况并非如此,因此您要么在脚本执行时以某种方式更改了工作目录,要么在其他目录中启动。你可以在命令行上用

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

但我不认为你在这样做。你说一些关于在Atom下运行脚本的事情,这可能会解释它。我没有这方面的经验,所以我不能随便说。请放

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.

作为程序中的第一行。然后你就会知道你从哪里开始以及当前目录是否正在改变。如果它没有改变,请尝试在 Atom 下和从命令行运行脚本,看看是否得到不同的结果。在我们确切地知道发生了什么之前,我们将无法解决问题。

回答by MingYi Ma

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

当我用 VS 编码时发生了同样的错误。我通过更改图像的路径来解决这个问题(此处使用绝对路径)。

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

更改pygame.image.load('/images/xxx')

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

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

我正在使用 mac,但在其他操作系统上应该是相同的。

回答by Perry Coxer

Just use the absolutepath 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')