视频图像批量处理脚本

1.单个文件夹下对图像进行批量裁剪

from PIL import Image
import os


def update(input_img_path, output_img_path):
    image = Image.open(input_img_path)
    cropped = image.crop((38, 0, 580, 610))  # 裁剪坐标为 (x0, y0, x1, y1)
    cropped.save(output_img_path)


dataset_dir = 'XXX'  # 需要裁剪的图片存放文件夹
output_dir = 'XXX'  # 裁剪后的图片输出文件夹

# 获取需要转换的图片路径并生成目标路径
image_filenames = [(os.path.join(dataset_dir, x), os.path.join(output_dir, x))
                   for x in os.listdir(dataset_dir)]
# 转换所有图片
for path in image_filenames:
    update(path[0], path[1])

2.历遍多个文件夹下对图像进行批量裁剪

from PIL import Image
import os


def update(input_img_path, output_img_path):
    image = Image.open(input_img_path)
    cropped = image.crop((38, 0, 580, 610))  # 裁剪坐标为 (x0, y0, x1, y1)
    cropped.save(output_img_path)


output_dir = 'output_folder'  # 裁剪后的图片输出文件夹

os.makedirs(output_dir, exist_ok=True)  # 创建输出文件夹

# 定义文件夹数量和命名规则
num_folders = 26
folder_format = 'XXX/{}'  # 文件夹路径的格式,{}会被替换为文件夹序号

# 遍历文件夹序号
for i in range(num_folders):
    dataset_dir = folder_format.format(i)  # 根据文件夹序号生成文件夹路径

    # 创建对应的输出子文件夹
    output_subdir = os.path.join(output_dir, os.path.basename(dataset_dir))
    os.makedirs(output_subdir, exist_ok=True)

    # 获取需要转换的图片路径并生成目标路径
    image_filenames = [(os.path.join(dataset_dir, x), os.path.join(output_subdir, x))
                       for x in os.listdir(dataset_dir)]
    # 转换并保存所有图片
    for path in image_filenames:
        update(path[0], path[1])

3.多个文件下的图像命名按文件名+排序方式命名

import os

def rename(path):
    file_list=os.listdir(path)
    i=0
    for fi in file_list:
        old_name=os.path.join(path,fi)
        new_name=os.path.join(path,str(i))
        os.rename(old_name,new_name)
        i+=1
def rename_images_in_folders(root_folder):
    folder_list = os.listdir(root_folder)
    for folder_name in folder_list:
        folder_path = os.path.join(root_folder, folder_name)
        if os.path.isdir(folder_path):
            image_list = os.listdir(folder_path)
            for i, image_name in enumerate(image_list):
                if image_name.endswith(('.jpg', '.jpeg', '.png')):
                    old_image_path = os.path.join(folder_path, image_name)
                    new_image_name = f"{folder_name}-{i}.jpg"
                    new_image_path = os.path.join(folder_path, new_image_name)
                    os.rename(old_image_path, new_image_path)

if __name__ == "__main__":

    path="./dataset"
    rename(path)
    root_folder = "./dataset"  # 根文件夹路径,包含多个子文件夹
    rename_images_in_folders(root_folder)


4.多个视频剪切成图像

import cv2
import os
import concurrent.futures
from threading import Lock

# 输入视频路径
video_path = "./dataset/"
# 输出图像路径
pic_path = "./output/"
filelist = os.listdir(video_path)

# 线程池最大线程数
max_workers = 5

# 线程锁
lock = Lock()


def video2pic(filename):
    cnt = 0
    dnt = 0
    output_folder = pic_path + str(filename)

    # 线程锁,确保只有一个线程创建文件夹
    with lock:
        if not os.path.exists(output_folder):
            os.mkdir(output_folder)

    cap = cv2.VideoCapture(video_path + str(filename))  # 读入视频
    while True:
        ret, image = cap.read()
        if image is None:
            break

        w = image.shape[1]
        h = image.shape[0]

        if (cnt % 5) == 0:
            output_file = f"{output_folder}/{dnt}.jpg"
            cv2.imwrite(output_file, image)
            print(output_file)
            dnt += 1

        cnt += 1

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()


if __name__ == '__main__':
    with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
        # 提交任务到线程池
        futures = [executor.submit(video2pic, filename) for filename in filelist]
        # 等待所有任务完成
        concurrent.futures.wait(futures)


你可能感兴趣的:(python,人工智能)