Python 2.6:“无法打开图像”错误 [英] Python 2.6: "Couldn't open image" error

查看:534
本文介绍了Python 2.6:“无法打开图像”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我将要用于游戏的地图制作基础,但我无法将图像加载到屏幕上。我已经尝试使用likeame letter将绝对文件路径发送到图像,我尝试更改图像的名称,我尝试加载在我制作的其他程序中工作的不同图像,并且我已尝试将与脚本本身位于同一目录中的图像。什么都没有奏效。我看了几个遇到同样问题的人,比如为什么我的pygame图片没有加载?我无法找到问题的答案。图像无法加载。这是代码:

I'm trying to make the base for a map that I'm going to be using for a game, but I can't load the image onto the screen. I've tried sending in an absolute filepath to the image with likesame letter case, I've tried changing the name of the image, I've tried loading different images that worked in other programs I made, and I've tried putting the image in the same directory as the script itself. Nothing has worked yet. I looked at a few threads of people who were having the same problem, such as Why are my pygame images not loading? and I can't find the answer to my problem. The image will not load. Here's the code:

import sys, pygame
from pygame.locals import *

pygame.init()
size = (600, 400)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Practice Map")
walls = pygame.image.load("C:\Users\dylan\Desktop\Practice Game\brick.jpg")

x = 0
y = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == KEYDOWN and event.key == K_ESCAPE:
            sys.exit()
        if event.type == KEYDOWN and event.key == K_UP:
            y -= 20
        if event.type == KEYDOWN and event.key == K_DOWN:
            y += 20
        if event.type == KEYDOWN and event.key == K_LEFT:
            x -= 20
        if event.type == KEYDOWN and event.key == K_RIGHT:
            x += 20
        screen.fill((0,0,0))
        screen.blit(walls,(x, 330))
        # more bricks to go here later
        pygame.display.flip()

#end

和错误:

Traceback (most recent call last):
  File "C:\Users\dylan\Desktop\Practice Game\move.py", line 8, in <module>
    walls = pygame.image.load("C:\Users\dylan\Desktop\Practice Game\brick.jpg")
error: Couldn't open C:\Users\dylan\Desktop\Practice Gamerick.jpg

我正在使对于Python版本2.6,IDLE作为我的编辑器。

I'm using Python 2.6 with PyGame 1.9 for Python version 2.6 with IDLE as my editor.

推荐答案

这里的问题是你使用\作为路径名分隔符,但\也用作Python字符串中的转义字符。特别是, \ b 表示退格(或'\ x08')。您可以使用其他反斜杠,因为没有完全记录但可靠的行为,将未知的转义序列(如 \ X )视为反斜杠后跟 X

The problem here is that you're using \ as a pathname separator, but \ is also used as an escape character in Python strings. In particular, \b means "backspace" (or '\x08'). You get away with the other backslashes because of not-quite-documented-but-reliable behavior that unknown escape sequences like \X are treated as a backslash followed by an X.

有三种解决方案:


  1. 使用原始字符串,这意味着忽略Python字符串转义: rC:\ Users\dylan\Desktop\Practice Game\brick.jpg

  2. 逃避你的反斜杠:C:\\Users \\dylan\\Desktop \\ PracticePraice Game \\ brick.jpg

  3. 使用正斜杠代替:C:/ Users / dylan / Desktop / Practice Game /brick.jpg

  1. Use a raw string, which means Python string escapes are ignored: r"C:\Users\dylan\Desktop\Practice Game\brick.jpg".
  2. Escape your backslashes: "C:\\Users\\dylan\\Desktop\\Practice Game\\brick.jpg".
  3. Use forward slashes instead: "C:/Users/dylan/Desktop/Practice Game/brick.jpg".

如果你已经记住了Python转义序列列表,那么愿意依赖可能会改变但可能不会改变的功能,你可以在这里逃避 \b ,但应该清楚为什么其他三个是更好的想法长期运行。

If you've memorized the list of Python escape sequences, and are willing to rely on functionality that may change but probably won't, you can get away with only escaping the \b here, but it should be clear why the other three are better ideas in the long run.

虽然Windows路径名本身使用反斜杠分隔符,但所有内置和标准库Python函数以及第三方库中的大多数函数都非常开心让你使用正斜杠代替。 (这是因为Windows根本不允许在路径中使用正斜杠。)

While Windows pathnames do natively use backslash separators, all built-in and standard-library Python functions, and most functions in third-party libraries, are perfectly happy to let you use forward slashes instead. (This works because Windows doesn't allow forward slashes in paths at all.)

要了解其工作原理和原因,您可能需要尝试打印出字符串:

To understand how and why this works, you might want to try printing out the strings:

>>> print "C:\Users\dylan\Desktop\Practice Game\brick.jpg"
C:\Users\dylan\Desktop\Practice Gamrick.jpg
>>> print r"C:\Users\dylan\Desktop\Practice Game\brick.jpg"
C:\Users\dylan\Desktop\Practice Game\brick.jpg
>>> print "C:\\Users\\dylan\\Desktop\\Practice Game\\brick.jpg"
C:\Users\dylan\Desktop\Practice Game\brick.jpg
>>> print "C:/Users/dylan/Desktop/Practice Game/brick.jpg"
C:/Users/dylan/Desktop/Practice Game/brick.jpg

这篇关于Python 2.6:“无法打开图像”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆