Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 将pil gif图像转换为pygame surface,而不将其保存在磁盘上_Python 2.7_Pygame_Python Imaging Library_Gif - Fatal编程技术网

Python 2.7 将pil gif图像转换为pygame surface,而不将其保存在磁盘上

Python 2.7 将pil gif图像转换为pygame surface,而不将其保存在磁盘上,python-2.7,pygame,python-imaging-library,gif,Python 2.7,Pygame,Python Imaging Library,Gif,我想从服务器端获取图像,并通过pygame在客户端显示它们,而不是将每个帧保存在磁盘上。使用base64编码和解码传输图像。当从另一端接收数据时,它被解码为PIL图像。然后我可以成功地将它保存到一个临时文件中,用pygame打开它并显示它。但是,我想直接使用image.tostring()执行此操作,但没有显示任何内容。我还注意到,当我保存到临时文件图片时,属性为: 表面(32x32x32西南) 当我直接做的时候: 表面(32x32x8西南) 谢谢: import base64 from io

我想从服务器端获取图像,并通过pygame在客户端显示它们,而不是将每个帧保存在磁盘上。使用base64编码和解码传输图像。当从另一端接收数据时,它被解码为PIL图像。然后我可以成功地将它保存到一个临时文件中,用pygame打开它并显示它。但是,我想直接使用image.tostring()执行此操作,但没有显示任何内容。我还注意到,当我保存到临时文件图片时,属性为: 表面(32x32x32西南)

当我直接做的时候: 表面(32x32x8西南)

谢谢:

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()

您是否尝试过使用pygame.image.load[]使用类似文件的对象而不是字符串


您可以传递StringIO[]对象而不是文件。

surface=pygame.image.load1(im)。convert()yealds errorsurface=pygame.image.load1(im)。convert()AttributeError:“module”对象没有属性“load1”。您看到的1和2只是指向文档的链接。如果单击将看到的链接,则可以调用load传递文件对象,而不是文件的路径。如果您使用图像的内容构建一个StringIO并将其传递给加载,它应该可以工作。请看一个示例。