我正在尝试使用带有pygame.image.load
的pygame从base64字符串中加载图像,但出现此错误:TypeError: load_extended() takes no keyword arguments
。这是我的代码(我没有包含整个字符串,因为它类似于10k个字符):
photo = """R0lGODlhRgBGAPZUAAAAAAAAMwAAzAArAAArMwArzAAr/wBVm..."""
pepeImg = pygame.image.load(data=photo)
这是我在pygame.image.fromstring中遇到的错误:
Traceback (most recent call last):
File "C:/Users/mikur/PycharmProjects/timer/dodge.py", line 25, in <module>
pepeImg = pygame.image.fromstring(base64.b64decode(photo, 'UTF-8'), (70, 70), 'RGBA')
File "C:\Users\mikur\AppData\Local\Programs\Python\Python37-32\lib\base64.py", line 83, in b64decode
assert len(altchars) == 2, repr(altchars)
AssertionError: b'UTF-8'
代码如下:
photo = 'R0utCnHtEERJ3qQo960xPQgArITgK1l.....33sYy+72+NudrGfXe1inzveVVABFSQBYIgIBAA7'
pepeImg = pygame.image.fromstring(base64.b64decode(photo, 'UTF-8'), (70, 70), 'RGBA')
这是我正在尝试加载的图片(漂亮):
我使用此网站将其转换为base64:https://www.askapache.com/online-tools/base64-image-converter/
答案 0 :(得分:2)
将字符串读取到内存文件中,如下所示:
import io
import pygame
import base64
pygame.init()
screen = pygame.display.set_mode((200, 200))
photo = 'R0lGODlhRgBGAPZUAAAAAAAAMwAAzAArAAArMwArzAAr/wBVmQBVzABV/zMAADMAMzMrADMrMzMrmTMrzDMr/zNVADNVMzNVZjNVmTNVzDNV/zOAADOAMzOAZjOA/zOqM2YAM2YrAGYrM2ZVAGZVM2ZVZmZVmWZVzGZV/2aAAGaAM2aAZmaAmWaAzGaA/2aqAGaqM2aqZmaqmWaqzJkrAJkrM5lVAJlVM5lVZplVmZmAAJmAM5mAZpmAmZmAzJmqZpmqmZmqzJmq/5nVmZnVzMxVAMxVM8xVZsyAAMyAM8yAZsyqZsyqmcyqzMyq/8zVmczVzMzV/8z/zMz////VzP/V////zP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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eImg = pygame.image.load(output)
running = True
while running:
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
screen.fill((255, 255, 255))
screen.blit(pepeImg, (50, 50))
pygame.display.update()