优化使得每检索50张照片,输出一次已检索文件数:import os from PIL import Image from multiprocessing import Pool def is_black_image(file_path): with Image.open(file_path) as img: pixels = list(img.getdata()) if all(sum(pixel) == 0 for pixel in pixels): return True else: return False def delete_black_images(file_path): if is_black_image(file_path): os.remove(file_path) print('{} has del'.format(file_path)) def batch_delete_black_images(folder_path): file_list = [] for f in os.listdir(folder_path): file_path = os.path.join(folder_path, f) if os.path.isfile(file_path): file_list.append(file_path) with Pool() as p: p.map(delete_black_images, file_list) if __name__ == '__main__': batch_delete_black_images(r'F:\H18\Dfinal640.tif\8-2')
时间: 2024-03-10 09:49:50 浏览: 38
为了每检索50张照片输出一次已检索文件数,您可以在 `batch_delete_black_images` 函数中添加一个计数器来实现。具体地,您可以在函数中添加一个变量 `count`,每删除一张黑色图片时,将计数器加1,并且当计数器能够被50整除时,打印当前已经检索的文件数。下面是修改后的代码:
```
import os
from PIL import Image
from multiprocessing import Pool
def is_black_image(file_path):
with Image.open(file_path) as img:
pixels = list(img.getdata())
if all(sum(pixel) == 0 for pixel in pixels):
return True
else:
return False
def delete_black_images(file_path):
if is_black_image(file_path):
os.remove(file_path)
print('{} has del'.format(file_path))
return True
return False
def batch_delete_black_images(folder_path):
file_list = []
count = 0
for f in os.listdir(folder_path):
file_path = os.path.join(folder_path, f)
if os.path.isfile(file_path):
file_list.append(file_path)
with Pool() as p:
results = p.map(delete_black_images, file_list)
for result in results:
if result:
count += 1
if count % 50 == 0:
print('已检索 {} 个文件'.format(count))
if __name__ == '__main__':
batch_delete_black_images(r'F:\H18\Dfinal640.tif\8-2')
```
注意,为了避免多个进程同时打印计数器的值,我们在每50次删除操作后只打印计数器的值,而不是实时打印。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)