python 大量输入解决办法-----多进程
import os
import time
from PIL import Image
from multiprocessing import Pool
def get_file_path(path):
img_paths = []
dirs = os.listdir(path)
for file_dir in dirs:
file_path = os.path.join(path, file_dir)
img_names = os.listdir(file_path)
for img_name in img_names:
img_path = os.path.join(file_path, img_name)
img_paths.append(img_path)
return img_paths
def resize_image(file_name):
try:
img = Image.open(file_name)
new_img = img.resize((250, 250), Image.ANTIALIAS)
new_img.save(file_name)
except:
print(file_name)
if __name__ == '__main__':
start = time.time()
path = r'C:\Users\Alvin_Fang\Downloads\identities_0'
img_paths = get_file_path(path)
pool = Pool(6)
pool.map(resize_image, img_paths)
pool.close()
pool.join()
end = time.time()
print(end - start)
————————————————
版权声明:本文为CSDN博主「Alvin_FZW」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Alvin_FZW/article/details/82886004