2016-05-19 53 views
0

我試圖創建打開視頻和它下面的圖像的GUI顯示視頻:問題的Tkinter

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
# 
import Image, ImageTk 
import Tkinter, tkMessageBox 
import ttk 

import cv2 
import sys 

width, height = 800, 600 
banner = cv2.imread('../data/banner.png') 
b,g,r = cv2.split(banner) 
banner = cv2.merge((r,g,b)) 
im = Image.fromarray(banner) 
cap = cv2.VideoCapture('../data/sample.mov') 


root = Tkinter.Tk() 
root.bind('<Escape>', lambda e: root.quit()) 
root.title("Contador") 
root.columnconfigure(0, weight=1) 
root.rowconfigure(0, weight=1) 

lmain = Tkinter.Label(root) 
lmain.grid(row=0,column=0,sticky='nsew') 
bmain = Tkinter.Label(root) 
bmain.grid(row=1,column=0,sticky='nsew') 
baner = ImageTk.PhotoImage(image=im) 
bmain.configure(image=baner) 

def show_frame(): 
    _, frame = cap.read() 
    if frame is None: 
     return 
# labelWidth = root.winfo_screenwidth() 
# labelHeight = root.winfo_screenheight() 
# maxsize = (labelWidth, labelHeight) 
# frame = frame.resize(maxsize) 
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) 
    img = Image.fromarray(frame) 
    imgtk = ImageTk.PhotoImage(image=img) 
    lmain.imgtk = imgtk 
    lmain.configure(image=imgtk) 
    lmain.after(10, show_frame) 

show_frame() 
root.mainloop() 

時遇到的問題如下:

  • 我需要調整圖像大小以適應標籤。
  • 註釋的第一部分從這裏(how to fit image to label in Python)得到,但它進一步給出一個信道編號錯誤(線40)和向下的代碼給出在管線42中的NoneType誤差(線41)和無效型圖像(numpy的陣列)的
  • 調整窗口大小時,圖像和視頻不會改變大小

所以我需要爲這個Tkinter的代碼解決方案(或者甚至蟒蛇一個更好的框架)

+0

有你爲什麼要每幀後,以調整具體原因是什麼?爲什麼不做'root.bind(「」,resize_frame)'並且只在需要時調整大小?只要保持在同一屏幕上,'winfo_screenwidth()'和'winfo_screenheight()'調用應該總是返回相同的值。您正在調整「框架」,但您是否調整其父容器? – R4PH43L

+0

我調整每一幀,因爲視頻作爲比標籤大開頭,我還沒有找到它粘到標籤大小的方式。 –

回答

0

我發現了什麼問題。 原來我從另一個問題得到了行:

frame = frame.resize(maxsize) 

應該是:

frame = cv2.resize(frame, maxsize) 

而且,以獲取標籤的大小的命令是:

labelWidth = lmain.winfo_width() 
labelHeight = lmain.winfo_height() 

因爲

labelWidth = root.winfo_width() 
labelHeight = root.winfo_height() 

獲取根目錄的大小而不是標籤的大小。