【发布时间】:2021-03-09 03:03:48
【问题描述】:
如何读取视频文件并将该视频的帧显示为 Jupyter 笔记本中的 gif 动画而不写入文件?
【问题讨论】:
标签: python video jupyter-notebook gif
如何读取视频文件并将该视频的帧显示为 Jupyter 笔记本中的 gif 动画而不写入文件?
【问题讨论】:
标签: python video jupyter-notebook gif
此答案结合了有关读取视频文件并将其在内存中转换为 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())
【讨论】: