使用Hug API返回GIF图像(图像在浏览器上不显示动画)

2024-09-30 19:29:58 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我有以下代码

    @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只取GIF图像的第一帧并返回它。有没有其他方法可以将整个GIF图像流式传输回浏览器?在


Tags: path代码图像formatimgoutputgetos
1条回答
网友
1楼 · 发布于 2024-09-30 19:29:58

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

@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

相关问题 更多 >