使用python一一打开一个文件夹中的图像? [英] Open images from a folder one by one using python?

查看:389
本文介绍了使用python一一打开一个文件夹中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要一一打开一个文件夹中的图像,然后对图像进行一些处理,然后将其保存回另一个文件夹.我正在使用以下示例代码进行此操作.

Hi all I need to open images from a folder one by one do some processing on images and save them back to other folder. I am doing this using following sample code.

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")

有什么最好的方法吗?

谢谢!

推荐答案

想要多线程处理的声音.这是一个快速的版本,可以完成.

Sounds like you want multithreading. Here's a quick rev that'll do that.

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)

(使用多重处理代替Thread,请参阅该文档有关更多示例和信息)

(edit: use multiprocessing instead of Thread, see that doc for more examples and information)

这篇关于使用python一一打开一个文件夹中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆