【发布时间】:2017-10-21 21:29:03
【问题描述】:
我想从服务器端获取图像并通过 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()
【问题讨论】:
-
以here为例。
标签: python-2.7 pygame python-imaging-library gif