I'm making a tkinter application for editing video from web-camera.
I want to make two windows. One for the buttons and source video and another for edited video.
import tkinter as tk
import cv2
import PIL.Image, PIL.ImageTk
import time
class MainWindow(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.canvas = tk.Canvas(self, width=720, height=480)
self.canvas.pack()
self.vid = cv2.VideoCapture(0)
self.btn_snapshot = tk.Button(self, text="Take photo", width=20, command=self.snapshot)
self.btn_snapshot.pack(anchor=tk.CENTER, expand=True)
self.Slider_1 = tk.Scale(self, length=300.0, from_=0, to=200, orient=tk.HORIZONTAL)
self.Slider_1.pack()
self.button = tk.Button(self, text="Create new window",
command=self.create_window)
self.button.pack(side="top")
self.show_frame()
def show_frame(self):
_, frame = self.vid.read()
frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(cv2image))
self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW)
tk.Label(root).after(10, self.show_frame)
def create_window(self):
tl = tk.Toplevel(self)
tl.wm_title("Window new")
l = tk.Label(tl, text="This is window")
l.focus_force()
l.pack(side="top", fill="both", expand=True, padx=100, pady=100)
def snapshot(self):
pass
ret, frame = self.vid.get_frame() #will be soon
if ret:
cv2.imwrite("frame-" + time.strftime("%d-%m-%Y-%H-%M-%S") + ".jpg", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
if __name__ == "__main__":
root = tk.Tk()
main = MainWindow(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
As a first step I just want function "create_window" to show me video from webcam. But If I just copy and past the code of function "show_frame" it doesn't work in apropriate way. So, the question is how to add webcam video to the function "create_window"?