我想从服务器端获取图像并通过pygame在客户端显示它们,而不是将每个帧保存在磁盘上。使用base64编码和解码传输图像。当从另一侧接收数据时,它被解码为PIL图像。然后我可以成功将其保存到临时文件,用pygame打开并显示它。但我想直接使用image.tostring()但显示注意事项我也注意到,当我保存到临时文件图片时,属性是: 表面(32x32x32 SW)
当我直接这样做时: 表面(32x32x8 SW)
感谢:
import base64
from io import BytesIO
import pygame
from PIL import Image
def gif__to_string(filePath):
gif_file = filePath
content = base64.encodestring(open(gif_file, 'rb').read())
return content
def string_to_gif(imageStr):
gif_image = Image.open(BytesIO(base64.b64decode(imageStr)))
return gif_image
def show_gif_working(im):
black = (0, 0, 0)
white = (255, 255, 255)
pygame.init()
width = 350;
height = 400
imageX = 200; # x coordnate of image
imageY = 30; #
screen = pygame.display.set_mode((width, height))
screen.fill(black)
im.save('temp.gif')
surface = pygame.image.load("temp.gif").convert()
screen.blit(surface, (imageY, imageY))
pygame.display.flip()
def show_gif_not_working(im):
black = (0, 0, 0)
white = (255, 255, 255)
pygame.init()
width = 350;
height = 400
imageX = 200; # x coordnate of image
imageY = 30; #
screen = pygame.display.set_mode((width, height))
screen.fill(black)
mode = im.mode
size = im.size
data = im.tostring()
surface1 = pygame.image.fromstring(data, size, mode)
screen.blit(surface1, (imageX, imageY))
pygame.display.flip()
def main():
content = gif__to_string('face5.gif')
print content
myImage = string_to_gif(content)
# show_gif_working(myImage)
show_gif_not_working(myImage)
main()