目录
一、基于opencv拍摄视频并保存
二、对视频抽帧
三、对图片进行合帧
四、更改图片尺寸
五、批量更改图片格式,如jpg转化为png、png转jpeg
import cv2
cam = cv2.VideoCapture(0)
wid = int(cam.get(3))
hei = int(cam.get(4))
size = (wid,hei)
fps = 30
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out = cv2.VideoWriter()
out.open(r"/home/ucar/ucar_ws/src/video/11.mp4",fourcc, fps, size)
while True:
ret, frame = cam.read()
if not ret:
break
frame = cv2.flip(frame,1)
out.write(frame)
cv2.imshow("frame",frame)
key = cv2.waitKey(100)
if cv2.waitKey(25) & 0xFF == ord('1'): # 当按键 1 按下
break
cam.release()
out.release()
cv2.destroyAllWindows()
import cv2
import os
import shutil
def get_frame_from_video(video_name, interval):
"""
Args:
video_name:输入视频名字
interval: 保存图片的帧率间隔
Returns:
"""
# 保存图片的路径
save_path = video_name.split('.mp4')[0] + '/'
is_exists = os.path.exists(save_path)
if not is_exists:
os.makedirs(save_path)
print('path of %s is build' % save_path)
else:
shutil.rmtree(save_path)
os.makedirs(save_path)
print('path of %s already exist and rebuild' % save_path)
# 开始读视频
video_capture = cv2.VideoCapture(video_name)
i = 0
j = 0
while True:
success, frame = video_capture.read()
i += 1
if i % interval == 0:
# 保存图片
j += 1
save_name = save_path + str(j) + '_' + str(i) + '.jpg'
cv2.imwrite(save_name, frame)
print('JPEGImages of %s is saved' % save_name)
if not success:
print('video is all read')
break
if __name__ == '__main__':
# 视频文件名字
video_name = r'C:\Users\pc\Desktop\1.mp4' # 不具体到视频会报错
interval = 1
get_frame_from_video(video_name, interval)
import cv2
import os
import numpy as np
from PIL import Image
def frame2video(im_dir, video_dir, fps):
im_list = os.listdir(im_dir)
im_list.sort(key=lambda x: int(x.replace("frame", "").split('.')[0])) # 最好再看看图片顺序对不
img = Image.open(os.path.join(im_dir, im_list[0]))
img_size = img.size # 获得图片分辨率,im_dir文件夹下的图片分辨率需要一致
# fourcc = cv2.cv.CV_FOURCC('M','J','P','G') #opencv版本是2
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
# fourcc = cv2.VideoWriter_fourcc(*'XVID') #opencv版本是3
videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)
# count = 1
for i in im_list:
im_name = os.path.join(im_dir + i)
frame = cv2.imdecode(np.fromfile(im_name, dtype=np.uint8), -1)
videoWriter.write(frame)
# count+=1
# if (count == 200):
# print(im_name)
# break
videoWriter.release()
print('finish')
if __name__ == '__main__':
im_dir = 'C:/Users/pc/Desktop/image/' # 帧存放路径
video_dir = 'C:/Users/pc/Desktop/video/1.mp4' # 合成视频存放的路径
fps = 10 # 帧率,每秒钟帧数越多,所显示的动作就会越流畅
frame2video(im_dir, video_dir, fps)
# 导入需要的模块
from glob import glob
from PIL import Image
import os
# 图片路径
# 使用 glob模块 获得文件夹内所有jpg图像
img_path = glob(r"C:\Users\pc\Desktop\image\*.jpg") # 获取路径下的图片(注意,图片需要按数字顺序命名,如1.jpg、2.jpg等等)
# 存储(输出)路径
path_save = r'C:\Users\pc\Desktop\image_resize'
for i, file in enumerate(img_path):
name = os.path.join(path_save, "%d.jpg" % i)
im = Image.open(file)
# im.thumbnail((720,1280))
reim = im.resize((640, 480))
print(im.format, reim.size, reim.mode)
reim.save(name, im.format)
这里以为png转jpg为例,具体步骤如下:
1、将需要转化的JPG格式的图片放在一个文件夹中,需要注意的是只能放JPG格式的图片,然后在存图片的文件夹下新建一个记事本文件,可以将名称命名为“图片批量转化”
2、双击打开此记事本文件,然后输入内容“ren *.jpg *.png”,需要注意的是,两个星号的前面都有一个空格,然后将记事本进行保存,然后再将记事本后缀名修改为“bat”
3、接着用鼠标双击刚才新建的后缀名为“bat”的这个文件,然后会自动弹出一个窗口,不用理会,过几秒钟会自动消失,然后就可以将所有的JPG格式的图片转换成了png格式了