2017-03-04 67 views
1

您好我有以下代码返回GIF图像(图像不会在浏览器动画)

@hug.get('/getgif', output=hug.output_format.image('gif')) 
    def get(username: str, password: str): 
     dir_path = os.path.dirname(__file__) 
     img_path = os.path.join(dir_path, 'animation.gif') 
     img = Image.open(img_path) 
     return img 

我使用的拥抱,python3和PIL创建响应。返回到我的浏览器的图像完全没有提及。我猜测PIL只需要GIF图像的第一帧并返回。有没有其他方法可以将整个GIF图像传输回浏览器?

回答

0

你不需要用PIL加载gif。你只需要给函数提供正确的输出格式并返回图像的文件路径。看看这个例子:

@hug.get('/images', output=hug.output_format.file) 
def get_image(name: str): 
    img_file = os.path.join(IMG_ROOT_DIR, name) # type: str 
    return img_file