如何在python中使用tkinter显示视频文件(图像)的帧 [英] How to show a frame of a video file (image) with tkinter in python

查看:151
本文介绍了如何在python中使用tkinter显示视频文件(图像)的帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tkinter 显示视频帧(不是来自流).下一步是允许用户在视频中向后或向前获取帧的按钮.我不得不说,我对 Python 编程还很陌生.所以首先我阅读了以下文章:

I am trying to show video frames (not from a stream) with tkinter. The next step are buttons which allow the user to get a frame backward or forward in the video. I have to say that I am quite new in programming with python. So first I read the following articles:

Python 片段:将视频转换为图像 http://srand.fr/blog/python%20import%20video.html

Python snippets: Converting video to images http://srand.fr/blog/python%20import%20video.html

Tkinter PhotoImage 类:http://effbot.org/tkinterbook/photoimage.htm

The Tkinter PhotoImage Class: http://effbot.org/tkinterbook/photoimage.htm

问题是我不能用imageio或VideoFileClip转换的图片用tkinter photoimage显示.我收到以下错误:

The problem is that I can’t use the image converted with imageio or VideoFileClip to show it with tkinter photoimage. I get the following error:

_tkinter.TclError: image "[[  …(some numbers)…   ]]" doesn't exist

这是我的简单代码.我希望你能帮助我:)

Here is my simple code. I hope you can help me :)

from moviepy.editor import VideoFileClip
from tkinter import *
import pylab

vid =VideoFileClip("example.mp4")

window = Tk()
window.title("Choose Frame")
window.geometry ("900x600")

count =20

photo = vid.get_frame(count)
label =Label(window, image = photo)
label.pack()

其他代码,同样问题:

import imageio
from tkinter import *
import pylab

filename = './example.mp4'
vid = imageio.get_reader(filename,  'ffmpeg')

window = Tk()
window.title("Choose Frame")
window.geometry ("900x600")

count =20

photo = vid.get_data(count)
label =Label(window, image = photo)
label.pack()

推荐答案

这有点晚,但总比没有好.

This is a bit late but better late than never.

这是我找到并稍加修改的工作示例,它适用于.mp4",视频但不适用于.flv",不知道为什么.

Here is a working example I found and modify a little, this works with '.mp4', videos but not with '.flv', don't know why.

注意:

python 2.7 导入 Tkinter

python 2.7 import Tkinter

python 3 导入 tkinter

python 3 import tkinter

import Tkinter as tk
import threading
import imageio
from PIL import Image, ImageTk

video_name = "test_video.mp4" #This is your video file path
video = imageio.get_reader(video_name)

def stream(label):

    frame = 0
    for image in video.iter_data():
        frame += 1                                    #counter to save new frame number
        image_frame = Image.fromarray(image)          
        image_frame.save('FRAMES/frame_%d.png' % frame)      #if you need the frame you can save each frame to hd
        frame_image = ImageTk.PhotoImage(image_frame)
        label.config(image=frame_image)
        label.image = frame_image
        if frame == 40: break                         #after 40 frames stop, or remove this line for the entire video

if __name__ == "__main__":

    root = tk.Tk()
    my_label = tk.Label(root)
    my_label.pack()
    thread = threading.Thread(target=stream, args=(my_label,))
    thread.daemon = 1
    thread.start()
    root.mainloop()

这篇关于如何在python中使用tkinter显示视频文件(图像)的帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆