Tuesday, November 19, 2024
Google search engine
HomeLanguagesHow to show webcam in TkInter Window – Python

How to show webcam in TkInter Window – Python

Python offers various modules for creating GUI applications, out of which, Tkinter lets users do various tasks inside their app. Thus, while creating a GUI app, have you ever felt the need to let the user open the camera on a specific condition? Don’t know, how to achieve that. Continue reading this article further to know about how to open the camera in Tkinter.

Modules Required:

  • Tkinter: As the GUI application is created in Python, then you surely need the Tkinter module. It is one of the easiest and fastest ways of creating GUI applications. You can download the module Tkinter using the following command:
pip install tkinter
  • OpenCV: The Python module which is used for image and video processing is known as OpenCV. The video capture objects can also be captured through this module. The OpenCV module can be installed using the following command:
pip install opencv-python
  • Pillow: The module which uses histograms to extract some statistical data out of images is known as the Pillow module. The pillow module can be installed using the following command:
pip install pillow

Stepwise Implementation

Step 1: First of all, import the libraries, Tkinter, OpenCV, and PIL.Image and PIL.ImageTk. These are the libraries needed for opening the camera in your app.

Python3




from tkinter import *
import cv2
from PIL import Image, ImageTk


Step 2: In this step, we will get a video capture object for the camera.

Python3




cap = cv2.VideoCapture(0)


Step 3: Now, define the width and height in the variables and set it for the video capture object.

Python3




width, height =  # Width of camera, #Height of Camera
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)


Step 4: Then, create a GUI app and bind the GUI app with the Escape button, such that whenever the escape button is pressed, the app is closed.

Python3




app = Tk()
app.bind('<Escape>', lambda e: app.quit())


Step 5: Next, create a label to display on the app. This is the label on which the camera will be opened.

Python3




label_widget = Label(app)
label_widget.pack()


Step 6: Moreover, create a button that when pressed will open the camera on the app.

Python3




button1 = Button(app, text="Open Camera",
                 command=open_camera)
button1.pack()


Step 7: This is the final and main step which will open the camera. In this function first, we will capture the video frame by frame. Now, we need to set up the color space for the video. It can be achieved using the cvtColor() function and we will capture the latest frame and transform it into an image. And then we will convert the captured image to a photo image for displaying on the label on the GUI app.

Python3




def open_camera():
  
    # Capture the video frame by frame
    _, frame = vid.read()
  
    # Convert image from one color space to other
    opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
  
    # Capture the latest frame and transform to image
    captured_image = Image.fromarray(opencv_image)
  
    # Convert captured image to photoimage
    photo_image = ImageTk.PhotoImage(image=captured_image)
  
    # Displaying photoimage in the label
    label_widget.photo_image = photo_image
  
    # Configure image in the label
    label_widget.configure(image=photo_image)
  
    # Repeat the same process after every 10 seconds
    label_widget.after(10, open_camera)


Example of How to Open Camera in Tkinter

In this example, we have placed a button ‘Open Camera’ on the GUI app. Whenever that button is clicked, the camera will be opened, while whenever the escape button is pressed, the camera will shut down. Below is the complete code in one place for the reader’s ease so, that they can run the code and have some fun.

Python3




# Python program to open the
# camera in Tkinter
# Import the libraries,
# tkinter, cv2, Image and ImageTk
  
from tkinter import *
import cv2
from PIL import Image, ImageTk
  
# Define a video capture object
vid = cv2.VideoCapture(0)
  
# Declare the width and height in variables
width, height = 800, 600
  
# Set the width and height
vid.set(cv2.CAP_PROP_FRAME_WIDTH, width)
vid.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
  
# Create a GUI app
app = Tk()
  
# Bind the app with Escape keyboard to
# quit app whenever pressed
app.bind('<Escape>', lambda e: app.quit())
  
# Create a label and display it on app
label_widget = Label(app)
label_widget.pack()
  
# Create a function to open camera and
# display it in the label_widget on app
  
  
def open_camera():
  
    # Capture the video frame by frame
    _, frame = vid.read()
  
    # Convert image from one color space to other
    opencv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
  
    # Capture the latest frame and transform to image
    captured_image = Image.fromarray(opencv_image)
  
    # Convert captured image to photoimage
    photo_image = ImageTk.PhotoImage(image=captured_image)
  
    # Displaying photoimage in the label
    label_widget.photo_image = photo_image
  
    # Configure image in the label
    label_widget.configure(image=photo_image)
  
    # Repeat the same process after every 10 seconds
    label_widget.after(10, open_camera)
  
  
# Create a button to open the camera in GUI app
button1 = Button(app, text="Open Camera", command=open_camera)
button1.pack()
  
# Create an infinite loop for displaying app on screen
app.mainloop()


Output:

RELATED ARTICLES

Most Popular

Recent Comments