2016-08-04 3 views
0

tkinter로 비디오 프레임 (스트림이 아닌)을 표시하려고합니다. 다음 단계는 사용자가 비디오에서 프레임을 앞뒤로 가져올 수있게하는 버튼입니다. 나는 파이썬으로 프로그래밍 할 때 새로운 점을 말해야 만한다.파이썬에서 tkinter로 비디오 파일 (이미지)의 프레임을 표시하는 방법

파이썬 미리보기 : 이미지 변환 비디오 http://srand.fr/blog/python%20import%20video.html

Tkinter의 광 화상 클래스 : 그래서 처음 나는 다음과 같은 기사를 읽어 http://effbot.org/tkinterbook/photoimage.htm

문제는 내가 ImageIO에서 또는으로 변환 된 이미지를 사용할 수 없다는 것입니다 VideoFileClip은 tkinter 사진 이미지로 보여줍니다. 다음 오류가 발생합니다.

_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() 

답변

1

이 조금 늦었지만 결코보다 늦게이다.

다음은 '.mp4'비디오와 함께 작동하지만 '.flv'로는 작동하지 않는 약간의 동작 예제입니다. 이유를 모르겠습니다.

주 : 여기에 2.7 수입 Tkinter를

파이썬 3 수입 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() 
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() 
관련 문제