我的python代码未进行应有的迭代。工作目录中有六个图像文件,len(f)也提供了六个。但是for循环的实际迭代在执行两个循环后停止。
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
path = "D:\\_my_python\\Image_histogram_equalization\\source_imgs"
os.chdir(path)
print("Current Working Directory: " , os.getcwd())
files = []
for r, d, f in os.walk(path):
for file in f:
if '.jpg' in file:
files.append(os.path.join(r, file))
print("Processing %d files.." %len(f))
count = 0
for f in files:
g = f[:f.find(".jpg")] + "_CLAHE20.jpg"
print("Converting %s to %s..." % (f, g))
img = cv2.imread(f)
img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(8,8))
img_yuv[:,:,0] = clahe.apply(img_yuv[:,:,0])
img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
cv2.imwrite(g, img_output)
count = count + 1
else:
print("Process completed for %d files out of %d files. " % (count, len(f)))
它应该运行六个循环,因为文件夹中有六个图像,而len(f)也提供了六个图像。