我创建了一个for循环,循环遍历图像目录并调整每个图像的大小,然后将其保存到另一个目录.代码可以工作,但我正在try 将进程并行化,以使其速度更快.
这是调整大小函数
import cv2
import os
def resize_image(img):
# get the name of the file
name = os.path.basename(img)
# read the image
img = cv2.imread(img)
# resize and save to new directory
resize = cv2.resize(img, (700, 700))
resized_image = cv2.imwrite("Resized/"+name, resize)
下面是for循环,它将遍历目录中的图像(调整目录中所有图像的大小大约需要700秒).
SOURCE_DIRECTORY = "Source/"
directory_list = os.listdir(SOURCE_DIRECTORY)
for source_file in directory_list:
source_path = os.path.join(SOURCE_DIRECTORY, source_file)
if os.path.isfile(source_path):
resize_image(source_path)
为了使进程并行化,我try 使用conCurent.Futures并将其映射到RESIZE函数.
import concurrent.futures
SOURCE_DIRECTORY = "Source/"
directory_list = os.listdir(SOURCE_DIRECTORY)
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(resize_image, directory_list)
但我立刻就发现了这个错误.
BrokenProcessPool: A child process terminated abruptly, the process pool is not usable anymore
我如何并行化调整图像大小的过程.任何帮助都将不胜感激.