import glob
import os
import cv2
import concurrent.futures
def load_and_resize(image_filename):
### Read in the image data
img = cv2.imread(image_filename)
### Resize the image
img = cv2.resize(img, (600, 600))
### Create a pool of processes. By default, one is created for each CPU in your machine.
with concurrent.futures.ProcessPoolExecutor() as executor:
### Get a list of files to process
image_files = glob.glob("*.jpg")
### Process the list of files, but split the work across the process pool to use all CPUs
### Loop through all jpg files in the current folder
### Resize each one to size 600x600
executor.map(load_and_resize, image_files)
从以上代码中摘出一行:
with concurrent.futures.ProcessPoolExecutor() as executor:
你的 CPU 核越多,启动的 Python 进程越多,我的 CPU 有 6 个核。实际处理代码如下:
executor.map(load_and_resize, image_files)
「executor.map()」将你想要运行的函数和列表作为输入,列表中的每个元素都是我们函数的单个输入。由于我们有 6 个核,我们将同时处理该列表中的 6 个项目!