Open images from a folder one by one using python?

前端 未结 2 1053
耶瑟儿~
耶瑟儿~ 2021-02-20 14:56

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.



        
2条回答
  •  逝去的感伤
    2021-02-20 15:47

    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)
    

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

提交回复
热议问题