多进程读取图片,查看图片的尺寸h,w是否为(1080,1920)
#coding:utf-8
import cv2
from tqdm import tqdm
from multiprocessing import Pool
def get_all_path(path_src, suffix):
Pathway = []
for r, ds, fs in os.walk(path_src):
for fn in fs:
if os.path.splitext(fn)[1] in suffix:
# print(fs)
# break
fname = os.path.join(r, fn)
# dst_namr = os.path.join(r, fn)
Pathway.append(fname)
return sorted(Pathway)
def process_image(img_path):
img = cv2.imread(img_path)
h, w, _ = img.shape
if h != 1080 or w != 1920:
print('shape', img_path, h, w)
if __name__ == '__main__':
# 图像路径列表
img_dir = '/data/cvorg_img'
process_num = 10 ## 进程数
img_pathway = get_all_path(img_dir, ['.jpg'])
# 创建进程池,根据需要设置进程数量
pool = Pool(processes=process_num )
# 使用进程池处理图像路径列表
with tqdm(total=len(img_pathway)) as pbar:
for _ in pool.imap_unordered(process_image, img_pathway):
pbar.update()
# 关闭进程池
pool.close()
pool.join()
本文介绍了如何使用Python的多进程技术,结合OpenCV库,遍历指定目录下的.jpg图片,检查其高度(h)和宽度(w)是否为1080x1920像素,以提高图像处理效率。

被折叠的 条评论
为什么被折叠?



