Python-OpenCV 从文件夹中批量读取图片

时间:2024-04-06 15:11:20

实现从文件夹批量读取图片并将图片裁剪为(128x128大小)

import os.path
import glob
import cv2
def convertjpg(jpgfile,outdir,width=128,height=128):
    
    src = cv2.imread(jpgfile, cv2.IMREAD_ANYCOLOR)
    
    try:
        dst = cv2.resize(src, (width,height), interpolation=cv2.INTER_CUBIC) 
        cv2.imwrite(os.path.join(outdir,os.path.basename(jpgfile)), dst)
    except Exception as e:
        print(e)
        
for jpgfile in glob.glob(r'C:\Users\x\Desktop\kk\*.jpg'):
    convertjpg(jpgfile,r'C:\Users\x\Desktop\re')

Python-OpenCV 从文件夹中批量读取图片