zoukankan      html  css  js  c++  java
  • Python常用代码功能

    Python常用代码功能

    提取一个文件夹下的所有图片(含子目录)到另一个文件夹下

    import os
    import cv2
    import shutil
    
    def getFileList(dir,Filelist, ext=None):
        """
        获取文件夹及其子文件夹中文件列表
        输入 dir:文件夹根目录
        输入 ext: 扩展名
        返回: 文件路径列表
        """
        newDir = dir
        if os.path.isfile(dir):
            if ext is None:
                Filelist.append(dir)
            else:
                if ext in dir[-3:]:
                    Filelist.append(dir)
        
        elif os.path.isdir(dir):
            for s in os.listdir(dir):
                newDir=os.path.join(dir,s)
                getFileList(newDir, Filelist, ext)
     
        return Filelist
     
    org_img_folder=r'E:documnetsMachine-Learning-Notes'
    new_img_folder=r'E:documnetsMachine-Learning-Note'
    # 检索文件
    imglist = getFileList(org_img_folder, [], 'jpg')
    print('本次执行检索到 '+str(len(imglist))+' 张图像
    ')
     
    for imgpath in imglist:
        imgname= os.path.splitext(os.path.basename(imgpath))[0]
        new_path = os.path.join(new_img_folder,imgname)+'.jpg'
        shutil.copy(imgpath,new_path)
        print(imgpath)
        #img = cv2.imread(imgpath, cv2.IMREAD_COLOR)
        # 对每幅图像执行相关操作
    
  • 相关阅读:
    如何查看ubuntu版本
    基于Python与命令行人脸识别项目(系列一)
    问题 B: Curriculum Vitae
    问题 M: 克隆玩具
    1906: 鹊桥相会
    3265: 聪明的矿工
    2363: 完美旗手队列
    2545: 内部收益率
    2544: 台球碰撞
    3272: 公民身份号码
  • 原文地址:https://www.cnblogs.com/lwp-nicol/p/14948692.html
Copyright © 2011-2022 走看看