Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to show webcam in TkInter Window?
Python libraries can be combined to create powerful applications. In this example, we will build an application using OpenCV and Tkinter to display webcam feed in a desktop window. OpenCV is a Python library for computer vision tasks, while Tkinter provides the GUI framework.
Prerequisites
Before creating the application, install the required packages ?
pip install opencv-python pip install Pillow
Make sure your system has a working webcam and the necessary permissions to access it.
How It Works
The application follows these steps ?
- Initialize OpenCV's
VideoCaptureto access the webcam - Capture frames continuously using
cv2.VideoCapture(0) - Convert each frame from BGR to RGB color format
- Use PIL to convert the frame into a Tkinter-compatible image
- Display the image in a Label widget and repeat the process
Complete Implementation
# Import required Libraries
from tkinter import *
from PIL import Image, ImageTk
import cv2
# Create an instance of TKinter Window or frame
win = Tk()
win.title("Webcam Display")
# Set the size of the window
win.geometry("700x350")
# Create a Label to capture the Video frames
label = Label(win)
label.grid(row=0, column=0)
# Initialize the webcam
cap = cv2.VideoCapture(0)
# Define function to show frame
def show_frames():
# Get the latest frame and convert into Image
ret, frame = cap.read()
if ret:
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(cv2image)
# Convert image to PhotoImage
imgtk = ImageTk.PhotoImage(image=img)
label.imgtk = imgtk
label.configure(image=imgtk)
# Repeat after an interval to capture continuously
label.after(20, show_frames)
# Function to release resources when window is closed
def on_closing():
cap.release()
win.destroy()
# Bind the closing event
win.protocol("WM_DELETE_WINDOW", on_closing)
# Start capturing frames
show_frames()
win.mainloop()
Key Points
-
cv2.VideoCapture(0)initializes the default webcam (index 0) -
cv2.COLOR_BGR2RGBconverts OpenCV's default BGR format to RGB for PIL -
label.after(20, show_frames)creates a 20ms delay between frames (?50 FPS) -
cap.release()properly releases webcam resources when closing - Error handling with
retensures frames are valid before processing
Troubleshooting
If the webcam doesn't work, try these solutions ?
- Check if another application is using the webcam
- Try different camera indices:
cv2.VideoCapture(1)orcv2.VideoCapture(2) - Verify webcam permissions in your system settings
- Test the webcam with other applications first
Conclusion
This implementation combines OpenCV for webcam access with Tkinter for GUI display. The key is converting frames to PIL-compatible format and using label.after() for smooth video streaming. Remember to release resources properly when closing the application.
