我试图把一个图像转换成一个字符串,再转换成一个字节,然后再转换成一个图像,但是某处有东西坏了,我真的不知道是什么。我使用的是python3.3和pygame。在
我之所以需要对字符串执行此操作,是因为稍后我计划将字符串导出到XML文件中,并且当我拉取变量时,它以unicode字符串类型出现。在
如果你能帮我解决这个烦人的问题,我将不胜感激。在
import pygame
pygame.init()
displaysurf = pygame.display.set_mode((500, 500))
dirtImg = pygame.image.load('1.gif')
dirtXY= dirtImg.get_size()
print(dirtXY)
dirtText = str(pygame.image.tostring(dirtImg,'RGBX',False))
dirtBytes = bytes(dirtText, 'utf8' )
print(type(dirtBytes))
#prints out <class 'bytes'>
testimg = pygame.image.fromstring(dirtBytes, dirtXY, 'RGBX')
错误信息
^{pr2}$显然,我不改变图像在任何时候,所以它必须在编码或解码字节。如果有更好的办法,那就告诉我
在与ddaniels的聊天中讨论后,我发现这是他正在使用的流程:
我认为复制粘贴对于其他人来说并不是一个通用的解决方案,所以我想办法从pygame映像到xml再回到pygame。在Python2中它更简单,但是这段代码同时适用于Python2和Python3。在
如果有人知道如何简化这个过程,请告诉我。在
相关问题 更多 >
编程相关推荐