造轮子之视频取图片

视频转图片有很多种方式哈,我用过ffmpeg,但是不是特别会用命令行,所以就自己简单写了一个。也没有时间进行整理,代码也不难,有看到我这篇博客的朋友可以自己研究一下,自己写代码的成就感还是很强的。

import cv2
import os
path = 'F:\\DATA\\video\\analogy\\'
# video_A_path = os.path.join(path,'trainA')
# video_B_path = os.path.join(path,'trainB')
def resizeImage(path, folder_name, video_name):
	image_path = os.path.join(path, folder_name, video_name) + '.jpg'
	resize_image_path = os.path.join(path, 'resize', folder_name, video_name) + '.jpg'
	image = cv2.imread(image_path)  
	dim = (448, 448)
	if(image is not None):
		resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
		cv2.imwrite(resize_image_path, resized)
		print('saving resize image' + resize_image_path)
def processImage(path, folder_name, video_name):
	video_path = os.path.join(path, folder_name, video_name)
	image_path1 = os.path.join(path, 'Image',folder_name, video_name)
	if not os.path.exists(image_path1):
		os.makedirs(image_path1)
	resize_image_path1 = os.path.join(path, 'ResizeImage', folder_name, video_name)
	if not os.path.exists(resize_image_path1):
		os.makedirs(resize_image_path1)
	vc = cv2.VideoCapture(video_path + '.mp4')
	print('reading video' + video_path)
	c=1

	if vc.isOpened():
		rval, frame = vc.read()
	else:
		rval = False

	timeF = 2
	index = 1

	while rval:
		rval, frame = vc.read()
		if(c%timeF==0):
			image_path2 = image_path1 + '\\' + str(index) + '.jpg'
			cv2.imwrite(image_path2,frame)
			print('saving'+image_path2)
			image = cv2.imread(image_path2)  
			dim = (256, 256)
			if(image is not None):
				resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
				resize_image_path2 = resize_image_path1 + '\\' + str(index) + '.jpg'
				index = index + 1
				cv2.imwrite(resize_image_path2, resized)
				print('saving resize image' + resize_image_path2)

		c= c+1

		cv2.waitKey(1)
	vc.release()
	'''
list_path = os.path.join(path,'list.txt')
with open(list_path, "r") as f:
	for line in f.readlines():
		VideoName = line.strip('\n')
		print(VideoName)
		processImage(path,'trainA',VideoName)
		processImage(path,'trainB',VideoName)
		'''
# processImage(path,'trainA','Fruit')
processImage(path,'trainB','Wolf')
# image = cv2.imread('F:\\DATA\\video\\Image\\trainB\\PlayVR\\502.jpg')
# print(image)
list_path = os.path.join(path,'list.txt')
with open(list_path, "r") as f:
	for line in f.readlines():
		VideoName = line.strip('\n')
		print(VideoName)
		resizeImage(path,'trainA',VideoName)
		resizeImage(path,'trainB',VideoName)

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值