Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter and OpenCV
#5
Thank you very much for your answer.
I found this solution to work but I don't understand everything that is being done. I will inquire.

import tkinter as tk
import cv2
from PIL import Image, ImageTk

width, height = 800, 600
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

root = tk.Tk()
root.bind("<Escape>", lambda e: root.quit())
lmain = tk.Label(root)
lmain.pack()

def show_frame():
    ref, frame = cap.read()
    frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(10, show_frame)

show_frame()
root.mainloop()
Reply


Messages In This Thread
Tkinter and OpenCV - by VHS - Jan-28-2021, 10:37 PM
RE: Tkinter and OpenCV - by Larz60+ - Jan-29-2021, 02:28 AM
RE: Tkinter and OpenCV - by VHS - Jan-29-2021, 10:52 AM
RE: Tkinter and OpenCV - by nilamo - Jan-29-2021, 06:58 PM
RE: Tkinter and OpenCV - by VHS - Jan-29-2021, 11:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python + OpenCV + Tkinter playing video help Hanban 1 18,015 May-08-2017, 01:25 AM
Last Post: Joseph_f2

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020