- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to show webcam in TkInter Window?
Python libraries are independent and thus they all can be used for different purposes while building a particular featured application. In this example, we will build an application using OpenCV and Tkinter library. OpenCV is a Python library that is used to work with Computer Vision and other artificial artifacts. Using the OpenCV module, we have to show the webcam in a tkinter window.
To create the application, you are required to install open-cv in your local machine and make sure that Python Pillow package is pre-installed. You can install these packages by typing the following commands,
pip install open-cv pip install Pillow
Once the installation has been done, we can start creating the structure and GUI of the application. The basic functionality of our application would be to open the web camera (if possible) using OpenCV. So, to display each and every captured frame, we can use Python Pillow (PIL) package which converts the frame into an Image. The Image now can be used in a Label widget that iteratively displays every captured frame in the window.
Example
# Import required Libraries from tkinter import * from PIL import Image, ImageTk import cv2 # Create an instance of TKinter Window or frame win = Tk() # 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) cap= cv2.VideoCapture(0) # Define function to show frame def show_frames(): # Get the latest frame and convert into Image cv2image= cv2.cvtColor(cap.read()[1],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 continiously label.after(20, show_frames) show_frames() win.mainloop()
Output
Whenever we run the above code, it will turn on the webcam and the output will display in the tkinter window.
- Related Articles
- How to show a window that was hidden using the "withdraw" method in Tkinter?
- How to draw images in the Tkinter window?
- How to control automated window resizing in Tkinter?
- Tkinter-How to get the current date to display in a tkinter window?
- How to keep the window focus on the new Toplevel() window in Tkinter?
- How to make Tkinter Window appear in the taskbar?
- How to put a Toplevel window in front of the main window in Tkinter?
- How to make a Tkinter window not resizable?
- How to delete Tkinter widgets from a window?
- How to add a margin to a tkinter window?
- Function to close the window in Tkinter
- How to bring Tkinter window in front of other windows?
- How to center a window on the screen in Tkinter?
- How to close only the TopLevel window in Python Tkinter?
- How to resize the background image to window size in Tkinter?
