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

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

问题描述

我正在尝试为我将用于游戏的地图制作基础,但我无法将图像加载到屏幕上.我尝试使用相同的字母大小写向图像发送绝对文件路径,我尝试更改图像的名称,我尝试加载在我制作的其他程序中工作的不同图像,并且我尝试将图像在与脚本本身相同的目录中.一切都还没有奏效.我查看了一些遇到相同问题的人的帖子,例如 为什么我的 pygame 图像没有加载吗? 我找不到问题的答案.图像不会加载.代码如下:

导入系统,pygame从 pygame.locals 导入 *pygame.init()大小 = (600, 400)屏幕 = pygame.display.set_mode(size)pygame.display.set_caption("练习地图")wall = pygame.image.load("C:UsersdylanDesktopPractice Gamerick.jpg")x = 0y = 0而真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:系统退出()如果 event.type == KEYDOWN 和 event.key == K_ESCAPE:系统退出()如果 event.type == KEYDOWN 和 event.key == K_UP:y -= 20如果 event.type == KEYDOWN 和 event.key == K_DOWN:y += 20如果 event.type == KEYDOWN 和 event.key == K_LEFT:x -= 20如果 event.type == KEYDOWN 和 event.key == K_RIGHT:x += 20屏幕填充((0,0,0))screen.blit(墙壁,(x,330))# 稍后再去这里的砖块pygame.display.flip()#结尾

和错误:

回溯(最近一次调用最后一次):文件C:UsersdylanDesktopPractice Gamemove.py",第 8 行,在 <module>wall = pygame.image.load("C:UsersdylanDesktopPractice Gamerick.jpg")错误:无法打开 C:UsersdylanDesktopPractice Gamerick.jpg

我将 Python 2.6 和 PyGame 1.9 用于 Python 2.6 版,并将 IDLE 作为我的编辑器.

解决方案

这里的问题是您使用 作为路径名分隔符,但 也用作 Python 字符串中的转义字符.特别是, 表示退格"(或 'x08').由于没有完全记录但可靠的行为,您可以摆脱其他反斜杠,即像 X 这样的未知转义序列被视为反斜杠后跟 X.

有三种解决方案:

  1. 使用原始字符串,这意味着忽略 Python 字符串转义:r"C:UsersdylanDesktopPractice Gamerick.jpg".
  2. 转义反斜杠:"C:\Users\dylan\Desktop\Practice Game\brick.jpg".
  3. 改用正斜杠:"C:/Users/dylan/Desktop/Practice Game/brick.jpg".

如果您已经记住了 Python 转义序列列表,并且愿意依赖可能会更改但可能不会更改的功能,那么您可以仅在此处转义 ,但从长远来看,为什么其他三个是更好的想法应该很清楚.

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

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

<预><代码>>>>打印C:UsersdylanDesktopPractice Gamerick.jpg"C:用户dylan桌面练习Gamrick.jpg>>>打印 r"C:UsersdylanDesktopPractice Gamerick.jpg"C:用户dylan桌面练习游戏rick.jpg>>>打印C:\用户\dylan\桌面\练习游戏\brick.jpg"C:用户dylan桌面练习游戏rick.jpg>>>打印C:/Users/dylan/Desktop/Practice Game/brick.jpg"C:/Users/dylan/Desktop/Practice Game/brick.jpg

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:UsersdylanDesktopPractice Gamerick.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

and the error:

Traceback (most recent call last):
  File "C:UsersdylanDesktopPractice Gamemove.py", line 8, in <module>
    walls = pygame.image.load("C:UsersdylanDesktopPractice Gamerick.jpg")
error: Couldn't open C:UsersdylanDesktopPractice Gamerick.jpg

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

解决方案

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,  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.

There are three solutions:

  1. Use a raw string, which means Python string escapes are ignored: r"C:UsersdylanDesktopPractice Gamerick.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".

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  here, but it should be clear why the other three are better ideas in the long run.

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:UsersdylanDesktopPractice Gamerick.jpg"
C:UsersdylanDesktopPractice Gamrick.jpg
>>> print r"C:UsersdylanDesktopPractice Gamerick.jpg"
C:UsersdylanDesktopPractice Gamerick.jpg
>>> print "C:\Users\dylan\Desktop\Practice Game\brick.jpg"
C:UsersdylanDesktopPractice Gamerick.jpg
>>> print "C:/Users/dylan/Desktop/Practice Game/brick.jpg"
C:/Users/dylan/Desktop/Practice Game/brick.jpg

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

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