我正在尝试使用tkinter显示视频帧(而不是来自流)。下一步是允许用户在视频中向后或向前获取帧的按钮。我不得不说我是用python编程的新手。 首先,我阅读以下文章:
Python代码段:将视频转换为图片http://srand.fr/blog/python%20import%20video.html
Tkinter PhotoImage类:http://effbot.org/tkinterbook/photoimage.htm
问题是我无法使用imageio或VideoFileClip转换的图像来显示tkinter photoimage。我收到以下错误:
_tkinter.TclError: image "[[ …(some numbers)… ]]" doesn't exist
这是我的简单代码。我希望你能帮助我:)。
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()
答案 0 :(得分:2)
这有点晚了但迟到总比没有好。
这是一个我发现并修改一点的工作示例,这适用于'.mp4',视频但不是'。flv',不知道为什么。
注意:
python 2.7 import Tkinter
python 3 import tkinter
C:\> pip install --upgrade tensorflow==1.1.0rc0
答案 1 :(得分:0)
这是我尝试使用Tkinter制作的另一个很好的工作示例,以及一些带Opencv模块的示例代码。 这只是一个例子,不是任何方式完成代码。
import cv2
from Tkinter import *
from PIL import Image, ImageTk
import io
import threading
import os, sys
def resize(image):
im = image
new_siz = siz
im.thumbnail(new_siz, Image.ANTIALIAS)
return im
def size(event):
global siz
if siz == screenWH:
siz = (200, 200)
else:
siz = screenWH
win.state('zoomed')
print 'size is: ', siz
def view_frame_video():
vc = cv2.VideoCapture('test_video.flv')
if vc.isOpened():
rval , frame = vc.read()
else:
rval = False
while rval:
rval, frame = vc.read()
img =Image.fromarray(frame)
img = resize(img)
imgtk = ImageTk.PhotoImage(img)
lbl.config(image=imgtk)
lbl.img = imgtk
if stop == True:
vc.release()
break #stop the loop thus stops updating the label and reading imagge frames
cv2.waitKey(1)
vc.release()
def stop_():
global stop
stop = True
def play():
stop = False
t = threading.Thread(target=view_frame_video)
t.start()
win = Tk()
stop = None
screenWH = (win.winfo_screenwidth(), win.winfo_screenheight())
siz = (200, 200)
Label(text='Press Play Button').pack()
frm_ = Frame(bg='black')
frm_.pack()
lbl = Label(frm_, bg='black')
lbl.pack(expand=True)
lbl.bind('<Double-Button-1>', size)
frm = Frame()
frm.pack()
Button(text='Play', command = play).pack(side=LEFT)
Button(text='Stop', command = stop_).pack(side=LEFT)
win.mainloop()