python遍历文件夹中的所有图像(按名称顺序读取)并存入本地文件夹

## 遍历一个文件夹下的所有图像 
def bianli_pics(path):
	import os
    img_folder = path
    img_list = [os.path.join(nm) for nm in os.listdir(img_folder) if nm[-3:] in ['jpg', 'png', 'gif']]
    ## print(img_list) 将所有图像遍历并存入一个列表
    ## ['test_14.jpg', 'test_15.jpg', 'test_9.jpg', 'test_17.jpg', 'test_16.jpg']
    for i in img_list:
          
        path=os.path.join(path,i)
        ## print(path)
        ## ./input/test_14.jpg
		## ./input/test_15.jpg
        image = cv2.imread(path). ## 逐个读取
if __name__=="__main__":
	path="./input"
	bianli_pics(path)
2. python遍历文件夹中的所有图像(按照文件名称顺序)
# -*- coding: utf-8 -*-
import os
base_path = r'./test_pics'
files = os.listdir(base_path)
files.remove('.DS_Store') ## Mac系统中可能会存在.DS_Store,提前将其删除
files.sort(key=lambda x: int(x.split('.')[0])) ## 使用切片将图片名称单独切开
for path in files:
    full_path = os.path.join(base_path, path)
    # print(full_path)
    with open(full_path) as fp:
        data = fp.read()
        print(data)
 
3. 将生成的图像保存至本地文件夹
	import cv2,os
	def save_2_local(image):
		base_name=os.path.basename(image) ## 获取图像的后缀名称
		new_image_path="./output/"+base_name   # 要存入的新路径和名称 >>basename,test_3.jpg
	    cv2.imwrite(new_image_path, image)  ## 存入的图像image

猜你喜欢

转载自blog.csdn.net/wxy2020915/article/details/129385911