Last active
August 24, 2022 13:56
-
-
Save rdenadai/bdd5b3499ce806a5d7ccdcae94090842 to your computer and use it in GitHub Desktop.
Open a window with webcam image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import Label, Tk | |
import cv2 | |
import PIL | |
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() | |
root.title("Camera") | |
root.wm_attributes("-topmost", 1) | |
root.bind("<Escape>", lambda e: root.quit()) | |
lmain = Label(root) | |
lmain.pack() | |
def show_frame(): | |
_, frame = cap.read() | |
frame = cv2.flip(frame, 1) | |
frame = cv2.resize(frame, (640, 480)) | |
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) | |
cv2image = cv2.flip(cv2image, 1) | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment