使用python逐个打开文件夹中的图像?

use*_*019 10 python python-imaging-library

大家好我需要逐个打开文件夹中的图像对图像进行一些处理并将它们保存回其他文件夹.我正在使用以下示例代码执行此操作.

path1 = path of folder of images    
path2 = path of folder to save images    

listing = os.listdir(path1)    
for file in listing:
    im = Image.open(path1 + file)    
    im.resize((50,50))                % need to do some more processing here             
    im.save(path2 + file, "JPEG")
Run Code Online (Sandbox Code Playgroud)

有没有最好的办法呢?

谢谢!

Chr*_*nus 12

听起来你想要多线程.这是一个快速的转换,它会做到这一点.

from multiprocessing import Pool
import os

path1 = "some/path"
path2 = "some/other/path"

listing = os.listdir(path1)    

p = Pool(5) # process 5 images simultaneously

def process_fpath(path):
    im = Image.open(path1 + path)    
    im.resize((50,50))                # need to do some more processing here             
    im.save(os.path.join(path2,path), "JPEG")

p.map(process_fpath, listing)
Run Code Online (Sandbox Code Playgroud)

(编辑:使用多处理而不是Thread,请参阅该文档以获取更多示例和信息)