【问题标题】:Problems displaying video in tkinter在 tkinter 中显示视频的问题
【发布时间】:2016-06-11 01:55:02
【问题描述】:

我正在尝试创建一个可以打开视频及其下方图像的 GUI:

#!/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 行),并且代码更往下给出了一个 NoneType 错误(第 41 行)和一个无效类型的图像(numpy 数组)第 42 行
  • 调整窗口大小时图像和视频不会改变大小

所以我需要这个 tkinter 代码的解决方案(甚至是更好的 python 框架)

【问题讨论】:

  • 是否有特定原因要在每一帧后调整大小?为什么不 root.bind("&lt;Configure&gt;", resize_frame) 只在需要时调整大小?只要您停留在同一个屏幕上,winfo_screenwidth()winfo_screenheight() 调用就应该返回相同的值。您正在调整 frame 的大小,但您是在调整其父容器的大小吗?
  • 我正在调整每一帧的大小,因为视频一开始就比标签大,我还没有找到一种方法让它坚持标签大小。

标签: python tkinter typeerror image-resizing


【解决方案1】:

我发现了问题所在。 结果是我从另一个问题中得到的:

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

获取根的大小而不是标签的大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-30
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多