【问题标题】:How to display frames of a video file as gif in Jupyter notebook without writing file如何在 Jupyter 笔记本中将视频文件的帧显示为 gif 而无需写入文件
【发布时间】:2021-03-09 03:03:48
【问题描述】:

如何读取视频文件并将该视频的帧显示为 Jupyter 笔记本中的 gif 动画而不写入文件?

【问题讨论】:

    标签: python video jupyter-notebook gif


    【解决方案1】:

    此答案结合了有关读取视频文件并将其在内存中转换为 gif 以及如何在 Jupyter 环境中显示此字节对象的多个答案。很抱歉,我无法再次找到我使用的所有资源。

    import imageio
    import skimage
    import cv2
    from IPython.display import Image
    from io import BytesIO
    
    file = "video.mkv"
    capture = cv2.VideoCapture(file)
    
    frames = list()
    for frame_no in range(10):
        capture.set(1, frame_no)
        ret, frame = capture.read()
        assert ret
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frames.append(frame)
    gif_file = BytesIO()
    imageio.mimsave(gif_file, [skimage.img_as_ubyte(frame) for frame in frames], 'GIF', fps=2)
    Image(data=gif_file.getvalue())
    

    【讨论】:

      猜你喜欢
      • 2021-05-28
      • 2019-01-02
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多