Pygame PNG 图像看起来已损坏 [英] Pygame PNG image looks corrupt

查看:50
本文介绍了Pygame PNG 图像看起来已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照

然而,当我启动我的程序时,出现的只是一个大小和位置正确的小故障图像:

我正在使用此代码来显示图像(与上面链接的教程中给出的代码相同):

导入pygame导入操作系统_image_library = {}def get_image(路径):全局_image_library图像 = _image_library.get(path)如果图像 == 无:canonicalized_pa​​th = path.replace('/', os.sep).replace('\\', os.sep)图像 = pygame.image.load(canonicalized_pa​​th)_image_library[路径] = 图像返回图像pygame.init()屏幕 = pygame.display.set_mode((400, 300))完成 = 错误时钟 = pygame.time.Clock()虽然没有完成:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:完成 = 真screen.fill((255, 255, 255))screen.blit(get_image('ball.png'), (20, 20))pygame.display.flip()时钟滴答(60)

为什么会这样?代码中是否存在缺陷?我尝试用不同的 PNG 替换 PNG,但这只会改变故障图像的外观 - 图像仍然无法正确显示.我使用的是 OS X 10.11 El Capitan、Python 3.5.0 和 Pygame 1.9.2.

解决方案

正如@DJ McGoathem 所说,由于 SDL_image 库的不同版本,Pygame 在 El Capitan 中存在已知问题;Pygame 需要 1.2.10 版,但 El Capitan 有 1.2.12 版.这可以通过降级这个库来解决,我就是这样做的(需要 Brew):

  1. 此代码复制到名为sdl_image的文件中.rb
  2. 在保存该文件的目录中打开一个终端
  3. 运行 brew remove sdl_image 以卸载不兼容版本的库
  4. 运行 brew install -f sdl_image.rb 安装兼容版本的库

此后,我的程序正确渲染了图像.

I'm following this guide to try and display a basic PNG image inside a Pygame window. My image is a simple 150x150 green ball with no transparency called ball.png, located in the same directory as my Python script:

However, when I start my program, all that appears is a glitchy image of the correct size and location:

I am using this code to display the image (identical to the code given in the tutorial linked above):

import pygame
import os

_image_library = {}
def get_image(path):
        global _image_library
        image = _image_library.get(path)
        if image == None:
                canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
                image = pygame.image.load(canonicalized_path)
                _image_library[path] = image
        return image

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
clock = pygame.time.Clock()

while not done:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True
        screen.fill((255, 255, 255))
        screen.blit(get_image('ball.png'), (20, 20))
        pygame.display.flip()
        clock.tick(60)

Why would this happen? Is there a flaw in the code? I have tried replacing the PNG with different ones, but that just changes how the glitchy image looks - the image is still not displayed correctly. I'm using OS X 10.11 El Capitan, Python 3.5.0 and Pygame 1.9.2.

解决方案

As @DJ McGoathem said, Pygame has known issues with El Capitan due to differing versions of the SDL_image library; Pygame needs version 1.2.10 but El Capitan has 1.2.12. This can be solved by downgrading this library, which I did like this (requires Brew):

  1. Copy this code into a file called sdl_image.rb
  2. Open a Terminal inside the directory where you saved that file
  3. Run brew remove sdl_image to uninstall the incompatible version of the library
  4. Run brew install -f sdl_image.rb to install the compatible version of the library

After this, my program was rendering the image correctly.

这篇关于Pygame PNG 图像看起来已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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