我正在制作一个游戏,其中包括位于密码保护的.zip中的图像、文本和音频文件。我尝试使用pygame.image.load
来显示图像,如下所示:
from zipfile import ZipFile
import pygame
import pyganim
import sys
pygame.init()
root = pygame.display.set_mode((320, 240), 0, 32)
pygame.display.set_caption('image load test')
archive = ZipFile("spam.zip", 'r')
mcimg = archive.read("a.png", pwd=b'onlyforthedev')
mc = pygame.image.load(mcimg)
anime = pyganim.PygAnimation([(mcimg, 100),
(mcimg, 100)])
anime.play()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
windowSurface.fill((100, 50, 50))
anime.blit(root, (100, 50))
pygame.display.update()
这是我从下面得到的错误:
Traceback (most recent call last):
File "C:\Users\admin\Desktop\VERY IMPORTANT FOR GAME DISTRIBUTION\few.py",
line 41, in <module>
mc = pygame.image.load(mcimg)
pygame.error: File path '�PNG
' contains null characters
发布于 2018-09-12 05:36:22
函数pygame.image.load
可以从文件源加载图像。您可以传递文件名或类似Python文件的对象。
但是,实际上你给出了图像字节数。
要解决此问题,您可以将字节包装在io.Bytes
实例中,并将其用作类文件对象:
import zipfile
import io
with zipfile.ZipFile("spam.zip", 'r') as archive:
mcimg = archive.read("a.png", pwd=b'onlyforthedev')
mc = pygame.image.load(io.BytesIO(mcimg))
https://stackoverflow.com/questions/52284271
复制相似问题