tkinter 选择一个视频并展示出来

1.选择视频文件
from tkinter.filedialog import askdirectory,askopenfilename
from tkinter import *

def selectPath():
path_ = askopenfilename()
path.set(path_)

root = Tk()
path = StringVar()
Label(root,text = "目标路径:").grid(row = 0, column = 0)
Entry(root, textvariable = path).grid(row = 0, column = 1)
Button(root, text = "路径选择", command = selectPath).grid(row = 0, column = 2)

root.mainloop()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2、展示视频
from tkinter import *

import camera as camera
import cv2
from PIL import Image, ImageTk

def video_play():
while video.isOpened():
ret, frame = video.read() # 读取照片
# print('读取成功')
if ret == True:
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) # 转换颜色使播放时保持原有色彩
current_image = Image.fromarray(img).resize((540, 320)) # 将图像转换成Image对象
imgtk = ImageTk.PhotoImage(image=current_image)
movieLabel.imgtk = imgtk
movieLabel.config(image=imgtk)
movieLabel.update() # 每执行以此只显示一张图片,需要更新窗口实现视频播放

video = cv2.VideoCapture('1.mp4') # 使用opencv打开本地视频文件
root = Tk()
root.title('视频播放案例')
Label(root,text = "视频播放案例'").grid(row = 0, column = 0)
movieLabel = Label(root) # 创建一个用于播放视频的label容器

movieLabel.grid(row = 1, column = 0,padx=10, pady=10)

video_play() # 调用video_play实现视频播放

mainloop()
camera.release()
cv2.destroyAllWindonws()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
3、 选择一个视频并展示出来
from tkinter.filedialog import askdirectory,askopenfilename
from tkinter import *

import camera as camera
import cv2
from PIL import Image, ImageTk

 

def selectPath():
path_ = askopenfilename()
path.set(path_)

video_path = path_
if video_path[-3:] == "mp4":
video = cv2.VideoCapture(video_path)
while video.isOpened():
ret, frame = video.read() # 读取照片
# print('读取成功')
if ret == True:
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) # 转换颜色使播放时保持原有色彩
current_image = Image.fromarray(img).resize((540, 320)) # 将图像转换成Image对象
imgtk = ImageTk.PhotoImage(image=current_image)
movieLabel.imgtk = imgtk
movieLabel.config(image=imgtk)
movieLabel.update() # 每执行以此只显示一张图片,需要更新窗口实现视频播放

root = Tk()
path = StringVar()

# layout
Label(root,text = "目标路径:").grid(row = 0, column = 0)
Entry(root, textvariable = path).grid(row = 0, column = 1)
Button(root, text = "路径选择", command = selectPath).grid(row = 0, column = 2)

movieLabel = Label(root) # 创建一个用于播放视频的label容器
movieLabel.grid(row = 1, column = 0,padx=10, pady=10)


mainloop()
camera.release()
cv2.destroyAllWindonws()
————————————————
版权声明:本文为CSDN博主「qq_42775938」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42775938/article/details/120597363

posted @ 2022-06-13 13:21  苍月代表我  阅读(206)  评论(0)    收藏  举报