【问题标题】:pygame.error Unsupported image formatpygame.error 不支持的图像格式
【发布时间】:2013-08-19 16:40:18
【问题描述】:

使用 pygame '1.9.2pre' 运行 Python 3.3.0,遵循教程,python 新手,老实说看不到我哪里出错了,看起来与教程中的一样,但是它已经 4 岁了。感谢您的帮助!

我收到错误 - 两者都不支持的图像格式。我试过 jpg 和 png,版本规范说它都支持它们。

bif ="bg.jpg"
mif ="man.jpg"
import pygame, sys
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((1100,750),0,32)
background = pygame.image.load(bif).convert()
mouse_c = pygame.image.load(mif).convert_alpha()


Running = True

while Running:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            Running = False
            sys.exit()
            break

    screen.blit(background,(0,0))

    x,y = pygame.mouse.get_pos()
    x -= mouse_r.get_width()/2
    y -= mouse_r.get_height()/2

    screen.blit(mouse_r,(x,y))

    pygame.display.update()

【问题讨论】:

    标签: python image pygame exit


    【解决方案1】:

    我会假设 pygame 窗口由于您的代码中的错误而没有关闭。你可以退出python shell来退出pygame窗口,但是错误是这里的主要问题。

    如果您要像这样导入图像,请确保图像与 .py 文件位于同一文件夹或位置 我不明白你是从哪里弄来 mouse_r 的。

    试试这个:

    import pygame, sys
    from pygame.locals import *
    
    
    
    pygame.init()
    
    
    
    screen = pygame.display.set_mode((1100,750),0,32)
    
    background = pygame.image.load("bg.jpg").convert()
    mouse_c = pygame.image.load("man.jpg").convert_alpha()
    
    Running = True
    
    while Running:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                Running = False
                sys.exit()
                break
    
        screen.blit(background,(0,0))
    
        x,y = pygame.mouse.get_pos()
        x -= screen.get_width()/2
        y -= screen.get_height()/2
    
        screen.blit(mouse_c,(x,y))
    
        pygame.display.update()
    

    【讨论】:

    • 我现在可以推荐 2 个选项。完全删除 pygame 并在此处使用此链接重新安装:lfd.uci.edu/~gohlke/pythonlibs/#pygame ... 在这里您可以获取 python 3.3.0 的 pygame ... 在图像编辑器 (xnView) 中打开图像并保存为 bmp 图像类型,所以然后你可以毫不费力地加载到 pygame 中
    • 使用 bmp 图片?如果你运行 python shell,import pygame,并运行函数pygame.image.get_extended(),它返回什么数字?
    • 应该是对的。返回值 1 表示您应该能够加载所有图像格式。恐怕我不知道问题出在哪里。
    【解决方案2】:

    也许你在这里使用了错误的变量名:

    x,y = pygame.mouse.get_pos()
    x -= mouse_r.get_width()/2
    y -= mouse_r.get_height()/2
    
    screen.blit(mouse_r,(x,y))
    

    我之前看过same tutorial,看来mouse_r应该是mouse_c。

    编辑:

    在加载图像时尝试使用完整的目录路径名:

    import os
    bif = os.getcwd() + "\\bg.jpg"
    mif = os.getcwd() + "\\man.jpg"
    

    【讨论】:

    • 这里的真正问题似乎是 pygame 不支持 jpeg 格式,因为我的答案中的代码也不起作用。 Pygame 文档指出,如果没有完整的图像支持,pygame 将仅支持 bmp 图像类型。但这对我或其他许多人来说都不是问题。问题是为什么?
    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 2021-11-05
    • 2016-07-28
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2019-06-09
    相关资源
    最近更新 更多