我想迭代文件夹中的每个图像。目前我正在使用下面的方法,但这需要我专门指定每个图像文件名的路径,这些文件名又长又繁琐(不能重命名),这是下面的代码:
我正在使用 for 循环,并且希望保留 for 循环,因为我在加载图像后做了很多其他事情,并且没有时间重组。请帮忙!
for i in r:
path = r'C:\\Users\\prodi\\OneDrive\\Desktop\\HTLT_CAL\\TEST_IMG'
#Read in the image using it's special name and the iterator i and add it to previous img for average
img = io.imread(path + '\\Run1_000' + str(240+i) + '_38_24_' + str(4728+(i)*20)+'.tiff', as_gray = True)
import os
path = r'C:\\Users\\prodi\\OneDrive\\Desktop\\HTLT_CAL\\TEST_IMG'
# edit to use only the image file extensions yhat you want.
image_extensions = ['.bmp', '.bpg', '.cgm', '.gif', '.hdr', '.heif', '.ico', '.jfi', '.jfif', '.jif', '.jpe', '.jpeg', '.jpg', '.logluv', '.nrrd',
'.pam', '.pbm', '.pcx', '.pgm', '.png', '.pnm', '.ppm', '.rgbe', '.svg', '.targa', '.tga', '.tif', '.tiff', '.wbmp', '.webp', '.xbm', '.xpm', '.xyze']
def find_images(directory):
for root, _, filenames in os.walk(directory):
for filename in filenames:
if any(filename.endswith(ext) for ext in image_extensions):
yield os.path.join(root, filename)
for image_path in find_images(path):
img = io.imread(image_path, as_gray = True)
print(image_path)