首页 > 解决方案 > 使用小部件在 jupyter notebook 中显示 gif

问题描述

如何使用 python 小部件在 jupyeter notebook 中显示 gif。

我努力了:

gif_box = widgets.Image("sample.gif")

或者

gif_box = widgets.Video("sample.gif")

但接收器错误:

TypeError: __init__() takes 1 positional argument but 2 were given

无论我尝试什么都行不通

标签: pythonjupyter-notebookipywidgets

解决方案


您需要将图像读入文件处理程序并将其读入字节字符串,然后才能将其传递给小部件,如下所示:

# read file as bytes and get file handler. use `with` to ensure 
# your file is closed after you're done reading it
with open("sample.gif", "rb") as file:
    # read file as string into `image` 
    image = file.read()

widgets.Image(
    value=image,
    format='gif'
)

请参阅小部件的文档Image


推荐阅读