python批量裁剪并保存图片

使用opencv裁剪图片

原始图片:在这里插入图片描述

import osimport cv2 as cv
#自然写法
'''i=1000000
im_path=r'F:\cityperson\citypersons2voc-master\data\leftImg8bit\train\\'
filelist = os.listdir(im_path)
for file in filelist:    
    file_path=os.path.join(im_path,file)
    filenames=os.listdir(file_path)
       for imagename in filenames:
           image_path=os.path.join(file_path,imagename)
           im=cv.imread(image_path)        
           im=im[256:1024,0:1280]        
           baocun=r'F:\cityperson\citypersons2voc-master\data\leftImg8bit\train_1'        
           save_path=os.path.join(baocun,str(i)+'.jpg')        
           cv.imwrite(save_path,im)        
           i=i+1'''
           
 #定义函数写法<太不正规了,太菜了>
def clip_image(filelist,i,im_path):    
    for file in filelist:
        file_path=os.path.join(im_path,file)
        filenames=os.listdir(file_path)        
        for imagename in filenames:
            image_path=os.path.join(file_path,imagename) 
            im=cv.imread(image_path)
            im=im[256:1024,0:1280]#高、宽
            baocun=r'F:\cityperson\citypersons2voc-master\data\leftImg8bit\train_11'
            save_path=os.path.join(baocun,str(i)+'.jpg')            cv.imwrite(save_path,im)            
            i=i+1
def main():    
    i=1000000    
    im_path=r'F:\cityperson\citypersons2voc-master\data\leftImg8bit\train\\'    
    filelist = os.listdir(im_path)    
    clip_image(filelist,i,im_path)
if __name__ == "__main__":
     main()    
     print ('成功')

裁剪后图片:按图片坐标裁剪得到左下角区域,注意图片左上角为(0,0)
在这里插入图片描述

相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页