Insert Video In Tkinter With Code Examples

  • Updated
  • Posted in Programming
  • 5 mins read

Insert Video In Tkinter With Code Examples

This text will present you, through a sequence of examples, methods to repair the Insert Video In Tkinter drawback that happens in code.

import tkinter as tk, threading
import imageio
from PIL import Picture, ImageTk

video_name = "check.mkv" #That is your video file path
video = imageio.get_reader(video_name)

def stream(label):

    for picture in video.iter_data():
        frame_image = ImageTk.PhotoImage(Picture.fromarray(picture))
        label.picture = frame_image

if __name__ == "__main__":

    root = tk.Tk()
    my_label = tk.Label(root)
    thread = threading.Thread(goal=stream, args=(my_label,))
    thread.daemon = 1

One other methodology that’s described beneath with code examples can be utilized to sort out the identical problem Insert Video In Tkinter.

1 import tkinter
 2 import cv2
 3 import PIL.Picture, PIL.ImageTk
 4 import time
 6 class App:
 7     def __init__(self, window, window_title, video_source=0):
 8         self.window = window
 9         self.window.title(window_title)
10         self.video_source = video_source
12         # open video supply (by default this may attempt to open the pc webcam)
13         self.vid = MyVideoCapture(self.video_source)
15         # Create a canvas that may match the above video supply measurement
16         self.canvas = tkinter.Canvas(window, width = self.vid.width, top =
17         self.canvas.pack()
19         # Button that lets the person take a snapshot
20         self.btn_snapshot=tkinter.Button(window, textual content="Snapshot", width=50, command=self.snapshot)
21         self.btn_snapshot.pack(anchor=tkinter.CENTER, broaden=True)
23         # After it's referred to as as soon as, the replace methodology might be routinely referred to as each delay milliseconds
24         self.delay = 15
25         self.replace()
27         self.window.mainloop()
29     def snapshot(self):
30         # Get a body from the video supply
31         ret, body = self.vid.get_frame()
33         if ret:
34             cv2.imwrite("frame-" + time.strftime("%d-%m-%Y-%H-%M-%S") + ".jpg", cv2.cvtColor(body, cv2.COLOR_RGB2BGR))
36     def replace(self):
37         # Get a body from the video supply
38         ret, body = self.vid.get_frame()
40         if ret:
41             self.photograph = PIL.ImageTk.PhotoImage(picture = PIL.Picture.fromarray(body))
42             self.canvas.create_image(0, 0, picture = self.photograph, anchor = tkinter.NW)
44         self.window.after(self.delay, self.replace)
47 class MyVideoCapture:
48     def __init__(self, video_source=0):
49         # Open the video supply
50         self.vid = cv2.VideoCapture(video_source)
51         if not self.vid.isOpened():
52             elevate ValueError("Unable to open video supply", video_source)
54         # Get video supply width and top
55         self.width = self.vid.get(cv2.CAP_PROP_FRAME_WIDTH)
56 = self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
58     def get_frame(self):
59         if self.vid.isOpened():
60             ret, body = self.vid.learn()
61             if ret:
62                 # Return a boolean success flag and the present body transformed to BGR
63                 return (ret, cv2.cvtColor(body, cv2.COLOR_BGR2RGB))
64             else:
65                 return (ret, None)
66         else:
67             return (ret, None)
69     # Launch the video supply when the item is destroyed
70     def __del__(self):
71         if self.vid.isOpened():
72             self.vid.launch()
74 # Create a window and move it to the Utility object
75 App(tkinter.Tk(), "Tkinter and OpenCV")

We discovered methods to resolve the Insert Video In Tkinter by taking a look at a variety of various circumstances.

Can we add video in tkinter?

tkVideo is a Python module for taking part in movies in GUIs created with tkinter. It does so by binding to a tkinter. Label widget of the person’s alternative and quickly altering its picture object.21-Nov-2020

How do I show a video in tkinter window?

Utilizing the OpenCV module, we’ve to point out the webcam in a tkinter window. As soon as the set up has been carried out, we are able to begin creating the construction and GUI of the applying. The essential performance of our utility can be to open the online digicam (if attainable) utilizing OpenCV.08-Jun-2021

How do you insert a video into Python?

How do I open a video in Python?

Can you utilize tkinter and OpenCV collectively?

Utilizing Tkinter Library, we are able to create an interactive utility that makes use of OpenCV because the important a part of the applying. To create the applying, you’re required to put in OpenCV in your native machine and make it possible for Python Pillow bundle is pre-installed.07-Jun-2021

How do I add a display screen in Python?

Tips on how to Create Window in Python Utilizing Tkinter?

  • from tkinter import * # Import tkinter library in your Python Program.
  • Window = Tk() # Declare a window object utilizing Tk() methodology.
  • Window. mainloop() # Finish this system utilizing the mainloop() methodology for the window. This methodology holds the window lively.

What’s ImageTk in Python?

The ImageTk module comprises assist to create and modify Tkinter BitmapImage and PhotoImage objects from PIL pictures. For examples, see the demo packages within the Scripts listing.

Are you able to edit movies with Python?

MoviePy is a Python module for video modifying, which can be utilized for primary operations (like cuts, concatenations, title insertions), video compositing (a.okay.a. non-linear modifying), video processing, or to create superior results. It may well learn and write the commonest video codecs, together with GIF.29-Jan-2022

How do you be part of audio to video in Python?

  • Syntax : clip.set_audio(audio)
  • Argument : It takes AudioFileClip object as argument.
  • Return : It returns VideoFileClip object.

How do I present video on cv2?

Enjoying Video from file

  • import numpy as np.
  • import cv2 as cv.
  • cap = cv.VideoCapture(‘vtest.avi’)
  • whereas cap.isOpened():
  • # if body is learn accurately ret is True.
  • if not ret:
  • print(“Cannot obtain body (stream finish?). Exiting “)
  • break.

Leave a Reply