1.批量读取绝对路径下的所有同类型文件 keyword:Batch Fetching
import os
设置读取路径即文件夹名,路径里 / 和 \ 字符的用法:用 单 / 或者 \\
path = 'D:/hello/world/'
声明一个接收目录下文件的列表
jsons = []
遍历取得目录下的所有同类型文件
for root, dirs, files in os.walk(path):
for file in files:
if os.path.splitext(file)[1] == '.json':
jsons.append(os.path.join(root, file))
2.以接收字典的形式批量读取JS文件 keyword:read json dic
for json_file in jsons:
with open(json_file) as f:
json_f = json.load(f)
3.图片转视频 keyword:img2video
import cv2
import os
img_root = 'D:/img/'
fps = 6
size=(1920, 1080)
输出视频的尺寸要与输入的图片尺寸大小一致 如不一致,要先对图片尺寸做处理
转换为 mp4 格式
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
转换为 avi 格式
fourcc = cv2.VideoWriter_fourcc('I','4','2','0')
videoWriter = cv2.VideoWriter('D:/img/result.mp4', fourcc,fps, size)
批量读取目录下图片文件
images = []
for root, dirs, files in os.walk('D:/img/'):
for file in files:
if os.path.splitext(file)[1] == '.jpg':
images.append(os.path.join(root, file))
for image in images:
frame = cv2.imread(image)
videoWriter.write(frame)
videoWriter.release()