在python中讀取文件夾中部分文件的方法
import cv2
import os
data_path = '/home/pi/bg/' #需讀取文件的路徑
img_list = os.listdir(data_path)
save_path = '/home/pi/bg/'
if not os.path.exists(save_path):
os.mkdir(save_path)
for i in range(0, len(img_list)):
print(i)
img_full_path = os.path.join(data_path, img_list[i])
if os.path.exists(img_full_path) and img_full_path.endswith('_label.png'):
label = cv2.imread(img_full_path)
label = label[..., 2]
save_full_path = os.path.join(save_path, img_list[i][:-9] + 'label' + '.png')
cv2.imwrite(save_full_path, label)