python 视频转图片保存(图形化界面)


 
import cv2
import sys
import os
import numpy as np

from tkinter import *
from tkinter.filedialog import *
from tkinter import filedialog
import threading

class WidgetsDemo:
	def __init__(self):
		
		window = Tk()                 # 创建一个窗口
		#设置屏幕居中
		sw=window.winfo_screenwidth()
		sh=window.winfo_screenheight()
		ww=400
		wh=100
		x = (sw-ww) / 2
		y = (sh-wh) / 2
		window.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
		window.title("Widgets Demo")  # 设置标题
		
		
		frame1 = Frame(window)        # 创建一个框架
		frame1.pack(side=TOP)                 # 将框架frame1放置在window中
		self.label_1=Label(frame1,text = "视频路径:").grid(row = 0, column = 0)
		self.path_1=StringVar()
		self.entry_1=Entry(frame1, textvariable = self.path_1,width = 36)
		self.entry_1.grid(row = 0, column = 1)
		
		self.button_1=Button(frame1, text = "浏览", command = self.selectPath_1).grid(row = 0, column = 2)

		self.label_2=Label(frame1,text = "图片路径:").grid(row = 1, column = 0)
		self.path_2=StringVar()
		self.entry_2=Entry(frame1, textvariable = self.path_2,width = 36)
		self.entry_2.grid(row = 1, column = 1)
		self.button_2=Button(frame1, text = "浏览", command = self.selectPath_2).grid(row = 1, column = 2)
		
		
		frame2 = Frame(window)        # 另外创建一个框架
		frame2.pack()                 # 将框架frame2放置在window中
		self.button_4=Button(frame2, text = "运行", width = 12,command = self.generate)
		self.button_4.pack(side=TOP)
		
		window.mainloop()
		
	#设置显示路径
	def selectPath_1(self):
		path_ = filedialog.askopenfilename(title='打开文件', filetypes=[('视频', '*.MP4'), ('All Files', '*')])
		self.path_1.set(path_)
		
	def selectPath_2(self):
		path_ = askdirectory()
		self.path_2.set(path_)
		
		
	def newthread(self):
		#获取文件名
		video_path=self.entry_1.get()
		image_path=self.entry_2.get()
		folder_name=video_path.split('/')[-1][:-4]
		print(folder_name)
		print(video_path)
		video_capture = cv2.VideoCapture(video_path)

		capture_count=1
		capture_interval=3
		frame_count=1
		
		while True:
			ret, frame = video_capture.read()
			if ret==False:
				break
			else :
				if(capture_count%capture_interval == 0): 
					print(image_path+folder_name+'_'+str(frame_count) + '.jpg')
					cv2.imwrite(image_path+'/'+folder_name+'_'+str(frame_count) + '.jpg', frame)
					frame_count += 1
				capture_count += 1
   
 
		video_capture.release()
		cv2.destroyAllWindows()
		print('采集完成')
		
	def generate(self):
		th=threading.Thread(target=self.newthread)
		th.setDaemon(True)
		th.start()
		

		
WidgetsDemo()


 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值