python 图像批处理

使用python对大批量图像进行训练预处理:

1.编号

2.任意格式转成JPG格式

3.将图像尺寸缩放,使得长宽均在300~500个像素范围内 

# 批量编号、转jpg、尺寸缩放
def imgSetFormat():
    path = "C:\\mypic"
    fileList = os.listdir(path)
    maxSize = 500
    cnt = 0
    for file in fileList:
        #跳过文件夹
        OldDir = os.path.join(path, file)
        if os.path.isdir(OldDir):  continue
        NewDir = os.path.join(path, str(cnt).zfill(6) + '.jpg')
        img = cv.imread(OldDir)
        w,h = img.shape[0:2]
        if w > h:   dstImg = cv.resize(img, (int(h*maxSize/w), maxSize))
        else:       dstImg = cv.resize(img, (maxSize, int(w*maxSize/h)))
        cv.imwrite(NewDir, dstImg)
        os.remove(OldDir)
        cnt += 1
    print('%d items done.' % cnt)

 

你可能感兴趣的:(python 图像批处理)